如何将名称空间反序列化为类

时间:2011-10-07 07:19:43

标签: c# .net xml xml-serialization xml-namespaces

我需要反序列化这样的XML:

<?xml version="1.0" encoding="utf-8"?>
<ns0:Info xmlns:ns0="http://www.x.com/">
 <Name>Anthony</Name>
 <Job>Developer</Job>
</ns0:Info>

我使用XSD工具从XSD文档创建了类,但它无法识别NAMESPACES。我怎么能这样做?我怎样才能改变课程以识别它们?我应该改变XSD吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

//Add an empty namespace and empty value
ns.Add("", "");

//Create the serializer
XmlSerializer slz = new XmlSerializer(someType);

//Serialize the object with our own namespaces (notice the overload)
slz.Serialize(myXmlTextWriter, someObject, ns);