8

I'm trying to deserialize an XML file to an object array, but I'm receiving empty objects. My question looks similar to this: How to Deserialize xml to an array of objects? but I can't seem to create a class which inherits IXmlSerializable. That said, I don't think that approach is necessary.

Am I doing something wrong?

File Object

 [XmlType("file")]
    public class File
    {
        [XmlElement("id")]
        public string Id { get; set; }

        [XmlElement("company_name")]
        public string Company_Name { get; set; }

        [XmlElement("docs")]
        public HashSet<doc> Docs { get; set; }
    }

Doc Object

 [XmlType("doc")]
    public class Doc
    {
        [XmlElement("valA")]
        public string ValA { get; set; }

        [XmlElement("valB")]
        public string ValB { get; set; }
    }

XML

<?xml version="1.0" encoding="UTF-8"?>
  <files>
    <file>
      <id>12345</id>
      <company_name>Apple</company_name>
      <docs>
       <doc>
          <valA>Info</valA>
          <valB>More Info</valB>
       </doc>  
      </docs>
    </file>
    <file>
      <id>12345</id>
      <company_name>Microsoft</company_name>
      <docs>
       <doc>
          <valA>Even More Info</valA>
          <valB>Lots of it</valB>
       </doc>  
      </docs>
    </file>
  </files>

Deserialization code

XmlSerializer mySerializer = new XmlSerializer(typeof(File[]), new XmlRootAttribute("files"));
using (FileStream myFileStream = new FileStream("Files.xml", FileMode.Open))
{
    File[] r;
    r = (File[])mySerializer.Deserialize(myFileStream);
}

2 Answers 2

11

You have decorated your properties with XMLAttribute but they are elements in your XML. So, change all XMLAttribute to XmlElement.

[XmlType("file")]
public class File
{
    [XmlElement("id")]
    public string Id { get; set; }

    [XmlElement("company_name")]
    public string Company_Id { get; set; }

    [XmlArray("docs")]
    public HashSet<Doc> Docs { get; set; }
}

[XmlType("doc")]
public class Doc
{
    [XmlElement("valA")]
    public string ValA { get; set; }

    [XmlElement("valB")]
    public string ValB { get; set; }
}

Also you XML is not well formed. I guess this is typo though -

<company_name>Apple</company_id>
<company_name>Microsoft</company_id>

Ending tag should be company_name -

<company_name>Apple</company_name>
<company_name>Microsoft</company_name>
Sign up to request clarification or add additional context in comments.

4 Comments

I tried changing it to element, still returns blank array. Yeah, just a typo for the ending tag.
I tried this and it worked perfectly fine for me. Also public HashSet<doc> Docs { get; set; } should be public HashSet<Doc> Docs { get; set; }
Id and Company_Id are these also null?
Ah, it works now. The key part was the attribute->element change. There was also a typo. Thanks!
4

I would use xml parser..

XDocument doc=XDocument.Load(url);
File[] r=doc.Elements("file")
            .Select(f=>
             new File
             {
                  Id=f.Element("id").Value,
                  Company_Id=f.Element("company_name").Value,
                  Docs=new HashSet<Docs>(
                       f.Elements("docs")
                        .Elements("doc")
                        .Select(d=>
                              new Doc
                              {
                                   ValA=d.Element("valA").Value,
                                   ValB=d.Element("valB").Value
                              }))
                }).ToArray();

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.