'Error in the XML document (2, 2).' Not expected info xmlns = ''

0

Performing the example of this thread I find an error when deserializar the object location and room:

 static void Main(string[] args)
    {
            Location locations = new Location();
            Room rooms = new Room();

            XmlReaderSettings settings = new XmlReaderSettings
            {
                ConformanceLevel = ConformanceLevel.Fragment
            };

            using (var reader = XmlReader.Create(@"c:\temp\locations.xml", settings))
            {
                #region Deserializar location

                XmlSerializer deserializerLoc = new XmlSerializer(typeof(Location), new XmlRootAttribute("location"));

                locations = (Location)deserializerLoc.Deserialize(reader);

                Location xmlDataLocation = locations;

                #endregion

                #region Deserializar room

                XmlSerializer deserializerRom = new XmlSerializer(typeof(Room));

                rooms = (Room)deserializerRom.Deserialize(reader);

                Room xmlDataRoom = rooms;

                #endregion

                Console.Write("cuartos : " + rooms.ToString());

                Console.Write("locaciones: " + locations);

            }

        Console.ReadKey();
    }

What I have consulted is that a namespace is missing, but if the process is already done and another error occurs in the same line, choose to leave the namespace without the namespace.

<?xml version="1.0" encoding="utf-8" ?>
 <info >
  <locations>
   <location name="New York">
    <Buildings>
     <Building name="Building1">
      <Rooms>
        <Room name="Room1">
          <Capacity>18<Capacity>
        <Room>
        <Room name="Room2">
          <Capacity>6<Capacity>
        <Room>
      <Rooms>
    <Building>

    <Building name="Building2">
      <Rooms>
        <Room name="RoomA">
          <Capacity>18<Capacity>
        <Room>
      <Rooms>
    <Building>
  <Buildings>
<location>
<location name="London">
  <Buildings>
    <Building name="Building45">
      <Rooms>
        <Room name="Room5">
          <Capacity>6<Capacity>
        <Room>
      <Rooms>
    <Building>
  <Buildings>
  <location>
<locations>
</info>

The first question is why does this error happen?

the second question is well coded to take the data from the xml to the object, I need it for certain XML tags.

    
asked by ger 09.11.2018 в 17:51
source

1 answer

0

The root of the XML file is a <info> tag and in the code:

 XmlSerializer deserializerLoc = new XmlSerializer(typeof(Location), new XmlRootAttribute("location"));

You are telling the serializer that what you are going to find is a Location , that is why the error was not expected <info> .

You have to do as in the example:

var serializer = new XmlSerializer(typeof(Info));
    
answered by 09.11.2018 / 18:24
source