如何使用 XmlSerializer 反序列化带有命名空间的 xml 文档?
使用 XmlSerializer 反序列化文档时,未正确反序列化
文档
<?xml version=\"1.0\"?>
<ns0:ElementA xmlns:ns0=\"urn:some-namespace\">
<Prop1>Some Value</Prop1>
<Prop2>Some other value</Prop2>
</ns0:ElementA>
类
[XmlRoot(Namespace = "urn:some-namespace")]
public class ElementA
{
[XmlElement]
public string Prop1 { get; set; }
[XmlElement]
public string Prop2 { get; set; }
}
Prop1 和 Prop2 在反序列化结束时均为 null。
我无法更改文档的结构以摆脱名称空间,因此我需要在我这边正确处理反序列化。
为了重现目的,该文档已被简化
我应该如何设置 ElementA 上的属性以正确处理反序列化?
-- 这是重现问题的完整代码 --
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();
}
}
}
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初作为评论发布,因为我还没有验证它,但是:
与 不一样
所以要让它工作,你可能只需要:
Originally posted as a comment, since I haven't verified it, but:
is not the same as
So to get it working, you probably just need: