1

I have the following XML...

<configuration>
    <img name="name1" />
    <img name="name2" />
    <warn>
        <img name="warn1" />
    </warn>
</configuration>

...which I try to deserialize into...

[XmlType("img")]
public class ImageNameExceptionItemXml
{
    [XmlAttribute("name")]
    public string Filename;
}

[XmlRoot("configuration")]
public class ImageNameExceptionListXml: List<ImageNameExceptionItemXml>
{
    [XmlArray("warn")]
    [XmlArrayItem("img")]
    public ImageNameExceptionListXml WarnList { get; set; }
}

...but I end up with the WarnList property null.

I already tried...

[XmlElement("warn"}]
public ImageNameExceptionListXml WarnList { get; set; }

...or...

[XmlElement("warn"}]
public List<ImageNameExceptionItemXml> WarnList { get; set; }

...but I still end up with WarnList property null. Why is that?

4
  • The schema looks not good. Is it possible to change? Commented Feb 16, 2012 at 11:43
  • Not sure, but I don't think the Serializer likes deserializing the extra properties on what it think is an array. So this may be tricky without explicitly implementing IXmlSerializable. Commented Feb 16, 2012 at 11:51
  • @findcaiyzh I can only change the <warn> node. Maybe I can try <configuration> <img name="name1" /> <img name="name2" /> <warnImg name="warn1" /> </configuration> Commented Feb 16, 2012 at 11:55
  • @sjlewis the problem is XmlSerializer just allow one type in array/list.<configuration><imgs><img... /><img ... /></imgs><warn ... /> is fine. Commented Feb 16, 2012 at 12:00

2 Answers 2

4

Let's go to the problem. The warn is a "root" element, so, you have to transform it in a class too:

The xml:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <img name="name1" />
  <img name="name2" />
  <warn>
      <img name="warn1" />
      <img name="warn2" />
  </warn>
</configuration>

The class:

[XmlType("img")]
public class ImageNameExceptionItemXml 
{ 
    [XmlAttribute("name")]     
    public string Filename; 
}

[XmlType("warn")]
public class WarnExceptionItemXml
{
    [XmlElement("img")]
    public List<ImageNameExceptionItemXml> ImgList { get; set; }
}

[XmlRoot("configuration")]
public class ImageNameExceptionListXml
{
    [XmlElement("img")]
    public List<ImageNameExceptionItemXml> ImgList { get; set; }

    [XmlElement("warn")]
    public WarnExceptionItemXml WarnList { get; set; } 
}

And the deserialize test:

XmlSerializer xml = new XmlSerializer(typeof(ImageNameExceptionListXml));

ImageNameExceptionListXml teste = (ImageNameExceptionListXml)xml.Deserialize(new FileStream("XMLFile1.xml", FileMode.Open));
Sign up to request clarification or add additional context in comments.

3 Comments

Do you have to repeat the same name both in [XmlElement] and [XmlType]?
@svick nope, you can use only in [XmlElement], but is better to give the idea easier. And you using in a different class it's better to understand who class is connected to xml element.
0

XmlSerializer just allow one type in array/list. is fine. Post My test code:

[XmlType("img")]
[Serializable]
public class ImageNameExceptionItemXml
{
    [XmlAttribute("name")]
    public string Filename;
}

[XmlType("warn")]
[Serializable]
public class Warnning
{
    [XmlArrayItem(typeof(ImageNameExceptionItemXml))]
    public List<ImageNameExceptionItemXml> imgs { get; set; }
}

[XmlRoot("configuration")]
[Serializable]
public class ImageNameExceptionListXml
{
    [XmlArrayItem(typeof(Warnning))]
    public List<Warnning> warns{ get; set; }

    [XmlArrayItem(typeof(ImageNameExceptionItemXml))]
    public List<ImageNameExceptionItemXml> imgs { get; set; }

}

xml:

<configuration>
<imgs>
    <img name="name1" />
    <img name="name2" />
</imgs>
<warns>
    <warn>
      <imgs>
        <img name="warn2" />
        <img name="warn1" />
      </imgs>
    </warn>
    <warn>
      <imgs>
        <img name="warn3" />
        <img name="warn4" />
      </imgs>
    </warn>
 </warns>
</configuration>

1 Comment

Vinicius Ottoni's answer is better.

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.