C# 反序列化具有多个元素属性的 XML 文件 - 属性不会反序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14245846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:08:53  来源:igfitidea点击:

Deserializing XML File with multiple element attributes - attributes are not deserializing

c#.netxmlserialization

提问by Amazing Grace

Using C# .Net 4 -- XML Sample (Real sample has 6 attributes)

使用 C# .Net 4 -- XML 示例(真实示例有 6 个属性)

<TestXML>
  <TestElement attr1="MyAttr" attr2="1" DateAdded="">25</TestElement>
</TestXML>

For my class definition I have the following:

对于我的类定义,我有以下内容:

public class TestXML() {
   public TestXML() {}

   public int TestElement {get; set;}
   [XmlAttribute]
   public string attr1 {get; set;}
   [XmlAttribute]
   public string attr2 {get; set;}
   [XmlIgnore]
   public DateTime DateAdded {get; set;}
   [XmlAttribute("DateAdded")]
   public string dateadded {
      get{ return (DateAdded == null ? "" : DateAdded.ToString();}
      set{ if(!value.Equals("")) DateAdded = DateTime.Parse(value);}
   }
}

Now the code to deserialize:

现在反序列化的代码:

string xml = "<TestXML><TestElement attr1=\"MyAttr\" attr2=\"1\" DateAdded=\"\">26</TestElement></TestXML>"
using (StringReader sr = new StringReader(xml)) {
   XmlSerializer serializer = new XmlSerializer(typeof(TestXML));
   TestXML myxml = (TestXML)serializer.Deserialize(sr);
}

Now the result we get is(viewing object in VS):

现在我们得到的结果是(在VS中查看对象):

myxml
  attr1         |  null
  attr2         |  null
  TestElement   |  25

At a complete loss as to why the attributes will not deserialize.

完全不知道为什么属性不会反序列化。

采纳答案by Marc Gravell

To do that you need two levels:

为此,您需要两个级别:

[XmlRoot("TestXML")]
public class TestXml {
    [XmlElement("TestElement")]
    public TestElement TestElement { get; set; }
}

public class TestElement {
    [XmlText]
    public int Value {get;set;}

    [XmlAttribute]
    public string attr1 {get;set;}

    [XmlAttribute]
    public string attr2 {get;set;}
}

Note that the > 26 <may cause problems too (whitespace); you may need that to be a string instead of an int.

请注意,> 26 <也可能会导致问题(空格);你可能需要它是一个字符串而不是一个整数。

回答by Mir

You are defining the attributes on TestElementwhile they should be on TestXML. Example:

您正在定义属性 onTestElement而它们应该是 on TestXML。例子:

@"<TestXML attr1=""MyAttr"" attr2=""1"">
      <TestElement>26</TestElement>
  </TestXML>"

回答by sa_ddam213

I just ran a test serializing/deserializing your object and it seems to work fine

我刚刚运行了一个测试序列化/反序列化你的对象,它似乎工作正常

Test:

测试:

TestXML obj = new TestXML{ attr1 = "Attrib1", attr2 = "Attrib2", DateAdded = DateTime.Now, TestElement = 44};

XmlSerializer serializer = new XmlSerializer(typeof(TestXML));
using (FileStream stream = new FileStream(@"C:\StackOverflow.xml", FileMode.OpenOrCreate))
{
    serializer.Serialize(stream, obj);
}

using (FileStream stream = new FileStream(@"C:\StackOverflow.xml", FileMode.Open))
{
    TestXML myxml = (TestXML)serializer.Deserialize(stream);
}

all attributes deserialized ok.

所有属性反序列化正常。

Result:

结果:

enter image description here

在此处输入图片说明

Xml:

xml:

<?xml version="1.0"?>
<TestXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attr1="Attrib1" attr2="Attrib2" DateAdded="10/01/2013 9:46:23 a.m.">
  <TestElement>44</TestElement>
</TestXML>

回答by goku_da_master

As an additional note to the accepted answer. Make sure the xml element doesn't contain a nil="true" attribute like this:

作为已接受答案的附加说明。确保 xml 元素不包含像这样的 nil="true" 属性:

<TestXML>
  <TestElement attr1="MyAttr" attr2="1" DateAdded="" xsi:nil="true">25</TestElement>
</TestXML>

From my experience the deserializer won't deserialize attributes of an element marked as null (nil).

根据我的经验,反序列化器不会反序列化标记为 null (nil) 的元素的属性。