Xml 反序列化 - 在 xml 数据中的未知节点之后,所有字段仍为空

发布于 2024-10-12 01:51:58 字数 1791 浏览 5 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-10-19 01:51:58

评估顺序相关吗?

如果没有,请从所有属性的 XmlElementAttribute 中删除 Order 参数,它将正常反序列化,即:

[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName")]
public string blatt
{

    get { return _blatt; }

    set { _blatt = value; }

}

Is the order of evaluation relevant?

If not, remove the Order parameter from the XmlElementAttribute on all properties, and it will deserialize fine, i.e.:

[System.Xml.Serialization.XmlElementAttribute(DataType = "NCName")]
public string blatt
{

    get { return _blatt; }

    set { _blatt = value; }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文