我如何反序列化使用XmlSerializer的命名空间的XML文档?文档、序列化、空间、XmlSerializer

2023-09-05 01:21:23 作者:夏末╄→雨纷飞

在反序列化使用XmlSerializer的文件没有正确反序列化

文件

 < XML版本= \1.0 \&GT?;
< NS0:ElementA的xmlns:NS0 = \瓮:一些命名空间\>
    <为prop1>有的值小于; /为prop1>
    < Prop2>其他的一些值小于/ Prop2>
< / NS0:ElementA>
 

  [XmlRoot(命名空间=瓮:一些命名空间)]
公共类ElementA
{
    [的XmlElement]
    公共字符串为prop1 {获得;组; }

    [的XmlElement]
    公共字符串Prop2 {获得;组; }
}
 
C 序列化与反序列化

都为prop1和Prop2是在反序列化的结束空。

我不能改变文档的结构,以摆脱空间的,所以我需要妥善处理反序列化就在我身边。

该文件已被简化为生育的目的

我应该如何设置在ElementA的属性,正确处理反序列化??

- 这里是全$ C $下重现问题 -

 命名空间ConsoleApplication1
{
    使用系统;
    使用System.IO;
    使用的System.Xml.Serialization;

    公共类节目
    {
        [XmlRoot(命名空间=瓮:一些命名空间)]
        公共类ElementA
        {
            [的XmlElement]
            公共字符串为prop1 {获得;组; }

            [的XmlElement]
            公共字符串Prop2 {获得;组; }
        }

        静态无效的主要(字串[] args)
        {
            VAR元=
                < XML版本= \1.0 \>? + Environment.NewLine +
                < NS0:ElementA的xmlns:NS0 = \瓮:一些命名空间\>中+ Environment.NewLine +<为prop1>有的值小于; /为prop1>中+ Environment.NewLine +
                < Prop2>其他的一些值小于/ Prop2>中+ Environment.NewLine +
                &所述; / NS0:ElementA>中;

            XmlSerializer的串行= XmlSerializer.FromTypes(新[] {的typeof(ElementA)})[0];
            ElementA结果;

            使用(VAR读卡器=新StringReader(元))
            {
                结果= serializer.Deserialize(读卡器)作为ElementA;
            }

            Console.WriteLine(为prop1:+ result.Prop1);
            Console.WriteLine(Prop2:+ result.Prop2);
            Console.ReadKey();
        }
    }
}
 

解决方案

最初发布的评论,因为我还没有证实此事,但是:

 <为prop1>有的值小于; /为prop1>
 

是不一样的

 < NS0:为prop1>有的值小于/ NS0:为prop1>
 

因此​​,为了得到它的工作,你可能只需要:

  [的XmlElement(命名空间=)]
公共字符串为prop1 {获得;组; }

[的XmlElement(命名空间=)]
公共字符串Prop2 {获得;组; }
 

When deserializing a document using XmlSerializer are not deserialized correctly

Document

<?xml version=\"1.0\"?>
<ns0:ElementA xmlns:ns0=\"urn:some-namespace\">
    <Prop1>Some Value</Prop1>
    <Prop2>Some other value</Prop2>
</ns0:ElementA>

Class

[XmlRoot(Namespace = "urn:some-namespace")]
public class ElementA
{
    [XmlElement]
    public string Prop1 { get; set; }

    [XmlElement]
    public string Prop2 { get; set; }
}

Both Prop1 and Prop2 are null at the end of the deserialization.

I cannot change the structure of the document to get rid of the namespace so I need to handle the deserialization properly on my side.

The document has been simplified for the purpose of reproduction

How should I set the attributes on the ElementA to handle the deserialization correctly??

-- Here is the full code for reproducing the problem --

namespace ConsoleApplication1
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    public class Program
    {
        [XmlRoot(Namespace = "urn:some-namespace")]
        public class ElementA
        {
            [XmlElement]
            public string Prop1 { get; set; }

            [XmlElement]
            public string Prop2 { get; set; }
        }

        static void Main(string[] args)
        {
            var element =
                "<?xml version=\"1.0\"?>" + Environment.NewLine +
                "<ns0:ElementA xmlns:ns0=\"urn:some-namespace\">" + Environment.NewLine+                    "    <Prop1>Some Value</Prop1>" + Environment.NewLine +
                "    <Prop2>Some other value</Prop2>" + Environment.NewLine +
                "</ns0:ElementA>";

            XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(ElementA) })[0];
            ElementA result;

            using (var reader = new StringReader(element))
            {
                result = serializer.Deserialize(reader) as ElementA;
            }

            Console.WriteLine("Prop1: " + result.Prop1);
            Console.WriteLine("Prop2: " + result.Prop2);
            Console.ReadKey();
        }
    }
}

解决方案

Originally posted as a comment, since I haven't verified it, but:

<Prop1>Some Value</Prop1>

is not the same as

<ns0:Prop1>Some Value</ns0:Prop1>

So to get it working, you probably just need:

[XmlElement(Namespace="")]
public string Prop1 { get; set; }

[XmlElement(Namespace="")]
public string Prop2 { get; set; }