反序列化xml不会拾取元素

时间:2015-07-31 12:21:59

标签: c# xml-serialization

我正在尝试反序列化一些xml。这是:

<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>

我试过了:

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; }
}

但看起来Data类不被视为xml的一部分,因为它无法读取它。我得到所有其他元素(ids)但不是数据中定义的东西。 我哪里错了?

4 个答案:

答案 0 :(得分:1)

假定丢失</Info>标记是一个错字,您只需要XmlArrayXmlArrayItem

[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; }

}

答案 1 :(得分:0)

XML缺少Info的结束标记。此外,您需要在Field3类中定义Data属性,并添加命名空间&#39; http://bars&#39;它。

答案 2 :(得分:0)

如果您的xml缺少信息结束标记,意图包含数据元素,那么您的类应该类似于:

[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; }
}

请注意,在xml中,info对象基本上包含所有数据,而不是你设计类的FooBars。

答案 3 :(得分:0)

通过LinqPad - 您可以在myData.Info属性中看到Field属性为null,这是您的问题,对吧? <强>已更新

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; }
}