[英]Deserializing XML File with multiple element attributes - attributes are not deserializing
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. 完全丧失了为什么属性不会反序列化。
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); 注意> 26 <
也可能导致问题(空白); you may need that to be a string instead of an int. 您可能需要将其作为字符串而不是int。
You are defining the attributes on TestElement
while they should be on TestXML
. 您在TestElement
上定义属性,而它们应该在TestXML
。 Example: 例:
@"<TestXML attr1=""MyAttr"" attr2=""1"">
<TestElement>26</TestElement>
</TestXML>"
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: 结果:
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>
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)的元素的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.