Xml 反序列化 - 在 xml 数据中的未知节点之后,所有字段仍为空
我想使用 XmlSerializer 将 xml 字符串反序列化为对象。
xml 字符串包含额外的未知节点,这些节点未包含在我想要反序列化的对象类中。 反序列化后,未知节点之前的字段被填充(“ast”),但其之后的所有字段(“pfosten”不在对象类中)仍为空。
xml-string:
<Baum>
<ast>1</ast>
<pfosten>2</pfosten>
<wurzel>3</wurzel>
<blatt>4</blatt>
</Baum>
对象类:
[Serializable]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Baum
{
public Baum() { }
string _ast;
string _wurzel;
string _blatt;
[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName", Order = 0)]
public string ast
{
get { return _ast; }
set { _ast = value; }
}
[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName", Order = 1)]
public string wurzel
{
get { return _wurzel; }
set { _wurzel = value; }
}
[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName", Order = 2)]
public string blatt
{
get { return _blatt; }
set { _blatt = value; }
}
}
我的代码如下所示:
private object DeserializeString(Type t, string s)
{
object obj;
XmlSerializer serializer = new XmlSerializer(t);
serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
using (var reader = new StringReader(s))
{
obj = serializer.Deserialize(reader);
}
return (obj);
}
private void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
{
Debug.WriteLine("UnknownNode Name: {0}", e.Name);
}
在调试期间,我可以看到,在“pfosten”上以及每个后续节点上调用了 serializer_UnknownNode() 方法。
我针对 .Net 2.0 进行编程。
希望我提供了所有信息,并且有人可以帮助我!
非常感谢,和尚
i want to deserialize a xml-string to an object using XmlSerializer.
The xml-string contains additional unknown nodes, which are not covered by my object-class i want to deserialize to.
After deserialization, fields before the unknown node are filled ("ast"), but all fields after it ("pfosten" not in object-class) remain empty.
xml-string:
<Baum>
<ast>1</ast>
<pfosten>2</pfosten>
<wurzel>3</wurzel>
<blatt>4</blatt>
</Baum>
object-class:
[Serializable]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Baum
{
public Baum() { }
string _ast;
string _wurzel;
string _blatt;
[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName", Order = 0)]
public string ast
{
get { return _ast; }
set { _ast = value; }
}
[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName", Order = 1)]
public string wurzel
{
get { return _wurzel; }
set { _wurzel = value; }
}
[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName", Order = 2)]
public string blatt
{
get { return _blatt; }
set { _blatt = value; }
}
}
my code looks like that:
private object DeserializeString(Type t, string s)
{
object obj;
XmlSerializer serializer = new XmlSerializer(t);
serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
using (var reader = new StringReader(s))
{
obj = serializer.Deserialize(reader);
}
return (obj);
}
private void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
{
Debug.WriteLine("UnknownNode Name: {0}", e.Name);
}
During debug i can see, that the serializer_UnknownNode() method is called on "pfosten" and also for each following node.
I program against .Net 2.0.
Hope i provided all information and that someone can help me with this!
thanks a lot, monk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评估顺序相关吗?
如果没有,请从所有属性的
XmlElementAttribute
中删除Order
参数,它将正常反序列化,即:Is the order of evaluation relevant?
If not, remove the
Order
parameter from theXmlElementAttribute
on all properties, and it will deserialize fine, i.e.: