Cyan
Cyan

Reputation: 1138

Deserialize xml does not pick up element

I am trying to deserialize some xml. Here it is:

<FooBars xmlns="http://foos">
  <Id1 xmlns="http://bars">2</Id1>
  <Id2 xmlns="http://bars">7</Id2>
  <Info xmlns="http://bars">
    <Data>
      <Field1>text1</Field1>
      <Field2>text2</Field2>
      <Field3>text3</Field3>
    </Data>
    <Data>
      <Field1>text5</Field1>
      <Field2>text6</Field2>
      <Field3>text7</Field3>
    </Data>
  </Info>
</FooBars>

I tried this:

var myData =  (FooBars)serializer.Deserialize(foobars.CreateReader());
...


[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
    [XmlElement(ElementName = "Id1", Namespace = "http://bars")]
    public string Id1 { get; set; }

    [XmlElement(ElementName = "Id2", Namespace = "http://bars")]
    public string Id2 { get; set; }  

    [XmlElement(ElementName = "Info", Namespace = "http://bars")]
    public List<Data> Info { get; set; }

}

public class Data
{
    [XmlElement(ElementName = "Field1")]
    public string Field1 { get; set; }

    [XmlElement(ElementName = "Field2")]
    public string Field2 { get; set; }
}

But it looks like the Data class is not considered part of the xml, since it is not able to read it. I am getting all the other elements (ids) but not the things defined in Data. Where am I wrong?

Upvotes: 0

Views: 103

Answers (4)

jacoblambert
jacoblambert

Reputation: 787

via LinqPad - you can see the Field properties are null in the myData.Info property, and that is your problem, right? UPDATED:

void Main()
{
    string xmlString;
    string path = @"C:\Temp\exampleXmlSO.xml";

    using (StreamReader streamReader = File.OpenText(path))
    {
        xmlString = streamReader.ReadToEnd();
    }
    XmlSerializer serializer = new XmlSerializer(typeof(FooBars));


    using (StringReader stringReader = new StringReader(xmlString))
    {
        var myData = (FooBars)serializer.Deserialize(stringReader);
        Console.WriteLine(myData);
    }
}

[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
    [XmlElement(ElementName = "Id1", Namespace = "http://bars")]
    public string Id1 { get; set; }

    [XmlElement(ElementName = "Id2", Namespace = "http://bars")]
    public string Id2 { get; set; }

    [XmlArray(ElementName = "Info", Namespace = "http://bars"), XmlArrayItem("Data")] //<--
    public List<Data> Info { get; set; }

}


[Serializable]
public class Data
{
    [XmlElement(ElementName = "Field1")]
    public string Field1 { get; set; }

    [XmlElement(ElementName = "Field2")]
    public string Field2 { get; set; }

    [XmlIgnore]
    public string Field3 { get; set; }
}

Upvotes: 0

EZI
EZI

Reputation: 15364

Presuming missing </Info> tag is a typo, All you need is XmlArray and XmlArrayItem

[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
    [XmlElement(ElementName = "Id1", Namespace = "http://bars")]
    public string Id1 { get; set; }

    [XmlElement(ElementName = "Id2", Namespace = "http://bars")]
    public string Id2 { get; set; }

    [XmlArray(ElementName = "Info", Namespace = "http://bars"), XmlArrayItem("Data")] //<--
    public List<Data> Info { get; set; }

}

Upvotes: 1

avi
avi

Reputation: 912

If your xml is missing the info closing tag, meant to contain the Data elements then your classes should look something like:

[XmlRoot(ElementName = "FooBars", Namespace = "http://foos")]
public class FooBars
{
    [XmlElement(ElementName = "Id1", Namespace = "http://bars")]
    public string Id1 { get; set; }

    [XmlElement(ElementName = "Id2", Namespace = "http://bars")]
    public string Id2 { get; set; }  

    [XmlElement(ElementName = "Info", Namespace = "http://bars")]
    public Info Information { get;set; }

}

public class Info {
    [XmlElement(ElementName = "Data", Namespace = "")]
    public Info[] Info { get; set; }
}

public class Data
{
    [XmlElement(ElementName = "Field1")]
    public string Field1 { get; set; }

    [XmlElement(ElementName = "Field2")]
    public string Field2 { get; set; }
}

Notice how in your xml the info object basically contains all the data, not the FooBars like you designed the classes.

Upvotes: 0

Richard Schneider
Richard Schneider

Reputation: 35477

The XML is missing a closing tag for Info. Also, you need to defined the Field3 property in the Data class and add the namespace 'http://bars' to it.

Upvotes: 0

Related Questions