反序列化xml不拾取元素

本文关键字:元素 xml 反序列化 | 更新日期: 2023-09-27 18:05:40

我正在尝试反序列化一些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的一部分,因为它不能读取它。我得到所有其他元素(id),但不是在数据中定义的东西。我哪里错了?

反序列化xml不拾取元素

假设缺失的</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; }
}

XML缺少Info的结束标记。此外,您需要在Data类中定义Field3属性,并向其添加名称空间'http://bars'。

如果您的xml缺少info结束标记,意味着包含数据元素,那么您的类应该看起来像:

[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对象基本上包含所有数据,而不是像您设计的类那样的foobar。

via LinqPad -您可以看到myData.Info属性中的字段属性为空,这是您的问题,对吗? :

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; }
}
相关文章: