2

I am trying to deserialize an xml in c# which looks like this (shortened version):

<?xml version="1.0" encoding="UTF-8"?>
<map>
    <parts>
      <part name="default part">
        <objects count="1597">
            <object type="1" symbol="6">
                <coords count="130">
                    <coord x="-221595" y="-109687" flags="1"/>
                    <coord x="-221625" y="-109744"/>
                    <coord x="-221640" y="-109785"/>
                    <coord x="-221640" y="-109841" flags="1"/>
                    <coord x="-221655" y="-109969"/>
                    <coord x="-221655" y="-110040"/>
                    <coord x="-221640" y="-110164" flags="1"/>
                    <coord x="-221640" y="-110209"/>
                    <coord x="-221655" y="-110265"/>
                </coords>
                <pattern rotation="0">
                    <coord x="0" y="0"/>
                </pattern>
            </object>
          </objects>
        </part>
   </parts>
</map>

Using the classes below:

[XmlRoot("map")] 
public class Map {


    [XmlElement(ElementName = "parts")]
    public List<Part> parts { get; set; }

    public Map()
    {
         parts = new List<Part>();
    }


public class Part {

    [XmlElement(ElementName = "objects")]
    public List<KdToPostGISProject.Object> objects { get; set; }

    public Part()
    {
        objects = new List<KdToPostGISProject.Object>();
    }

    [XmlAttribute(AttributeName = "name")]
    public String name { get; set; }     

}

public class Object
    {
         [XmlElement(ElementName = "coords")]
         public List<Coord> coords { get; set; } 

          public Object()
          {
             coords = new List<Coord>();
          }
    }


public class Coord
{
    [XmlAttribute]
    public int x { get; set; }
    [XmlAttribute]
    public int y { get; set; }
}

And the main function:

var serializer = new XmlSerializer(typeof(Map), new XmlRootAttribute("map"));               

Map resultingMessage = (Map)serializer.Deserialize(new FileStream(@"myXml.xml", FileMode.Open));

For some reason which I've been trying to figure out I keep getting zero objects (and null name) in my part-class. Right now I'm stuck, anyone who has any input?

4
  • 1
    Try marking the classes as[Serializable]. Commented May 20, 2014 at 8:23
  • @Killercam doesn't seem to do the trick. Commented May 20, 2014 at 8:29
  • 2
    copy your xml and go to the edit menu in VS and then click on past special and past xml as classes I think your class declaration cause the prob Commented May 20, 2014 at 8:33
  • @DhavalPatel Thanks, that actully worked. Still don't get why my code doesn't though, but thanks! Commented May 20, 2014 at 8:37

1 Answer 1

5

For List<T> members, you have to mark them as e.g.:

[XmlArray(ElementName = "parts")]
[XmlArrayItem(ElementName = "part")]
public List<Part> parts { get; set; }

Not:

[XmlElement(ElementName = "parts")]

Change for all List<T> members then it should work fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.