简体   繁体   English

如何使用XML可序列化的名称空间前缀?

[英]How can I XML Serializable namespace prefixes?

This is XML 这是XML

<NS1:Response xmlns:NS1="http://www.opentravel.org/OTA/2003/05">
     <DATE CheckIn="2015-02-01"/>
</NS1:Response>

this Is Models 这是模特

[Serializable]
[XmlRoot(ElementName = "Response")]
public class Response
{
    [XmlElement(ElementName = "DATE")]
    public DATE DATE {get;set;}

    public class DATE
    {
        [XmlAttribute(AttributeName = "CheckIn")]
        public string CheckIn {get;set;}
    }
}

How can I Add NS1 Namespace prefix in Models? 如何在模型中添加NS1命名空间前缀? Please advise me. 请建议我。 Thank you very much. 非常感谢你。

Before I explain how to do what you want, it's important to realize that XML readers/parser generally don't care what prefixes you stick on your elements; 在我解释如何做你想做的事情之前,重要的是要认识到XML读取器/解析器通常并不关心在元素上使用的前缀是什么。 they only care about the full namespace . 他们只关心完整的名称空间

In other words, when your sample XML fragment is loaded, the ns1 bit is completely thrown away. 换句话说,当您加载示例XML片段时, ns1位将被完全丢弃。 Internally, what you get are XML namespace/element pairs, like ("http://www.opentravel.org/OTA/2003/05", "Response") or ("http://www.opentravel.org/OTA/2003/05", "Date") . 在内部,您得到的是XML名称空间/元素对,例如("http://www.opentravel.org/OTA/2003/05", "Response")("http://www.opentravel.org/OTA/2003/05", "Date") The reason this is important to know is because you can assign different namespace prefixes to the XML data for use by eg XPath, and it will work fine. 知道这一点很重要的原因是因为您可以为XML数据分配不同的名称空间前缀,以供XPath使用,并且它可以正常工作。 That is, I could read your XML fragment into my program, and say that "http://www.opentravel.org/OTA/2003/05" should map to the prefix "t" , and use XPath like //t:Response to get the correct results, even though the source XML data had not t prefix. 也就是说,我可以将您的XML片段读入程序中,并说"http://www.opentravel.org/OTA/2003/05"应映射到前缀"t" ,并使用类似//t:Response XPath //t:Response以得到正确的结果,即使源XML数据早已不是t前缀。

In other words, you really, really should not bother trying to get a specific XML namespace prefix into your XML, because it should not matter. 换句话说,您真的不应该费心尝试将特定的 XML名称空间前缀添加到XML中,因为这无关紧要。 If having a specific prefix is necessary for everything to work properly, then someone somewhere is doing something very wrong. 如果必须有一个特定的前缀才能使一切正常工作,那么某个地方的某人做错了什么。

Having said that, if for some reason you need to output specific namespace prefixes, or if you just happen to like the way they look, you can use the XmlSerializerNamespaces class, as so: 话虽如此,如果由于某种原因需要输出特定的名称空间前缀,或者只是碰巧喜欢它们的外观,则可以使用XmlSerializerNamespaces类,如下所示:

var ns = new XmlSerializerNamespaces(); 
ns.Add("NS1", "http://www.opentravel.org/OTA/2003/05");

var s = new XmlSerializer(typeof(Response));
var output = new StreamWriter(SOME_FILENAME);

s.Serialize(response, output, ns);

For this to work, you also have to decorate your classes with the full namespace you want them to be in. All of the XML Serializer attributes have a Namespace parameter you use for this purpose, eg: 为此,还必须用要它们放入的完整名称空间来装饰类。所有XML Serializer属性都有一个用于此目的的Namespace参数,例如:

[XmlRoot(ElementName = "Response",
         Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class Response
{
}

When you serialize your object, the serializer will look up the namespaces in the namespace map, and apply the prefix you selected to the appropriate elements. 序列化对象时,序列化程序将在名称空间映射中查找名称空间,并将所选的前缀应用于适当的元素。

The namespace prefix does not matter in XML. 名称空间前缀在XML中无关紧要。 The namespace assigned to the prefix is the key. 分配给前缀的名称空间是键。

You can use the XmlRoot attribute to assign the namespace. 您可以使用XmlRoot属性来分配名称空间。

[Serializable]
[XmlRoot(ElementName = "Response", 
         Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class Response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM