简体   繁体   English

更改/重命名名称空间前缀

[英]Change/rename namespace prefix

I have an XELEMENT that contains these elements 我有一个包含这些元素的XELEMENT

<customer>
    <ns8:title xmlns:ns8="http://ws.xxx.com/xxx/model/common">B< /ns8:title>
    <ns9:firstname xmlns:ns9="http://ws.xxx.com/xxx/model/common">Bob< /ns9:firstname>
    <ns10:lastname xmlns:ns10="http://ws.xxx.com/xxx/model/common">Petier< /ns10:lastname>
    <ns11:bday xmlns:ns11="http://ws.xxx.com/xxx/model/common">19310227< /ns11:bday>
    <ns12:rank xmlns:ns12="http://ws.xxx.com/xxx/model/common">1< /ns12:rank>
</customer>

I would like to transform the prefix like this. 我想像这样转换前缀。

<customer>
    <ns1:title xmlns:ns1="http://ws.xxx.com/xxx/model/common">B< /ns1:title>
    <ns2:firstname xmlns:ns2="http://ws.xxx.com/xxx/model/common">Bob< /ns2:firstname>
    <ns3:lastname xmlns:ns3="http://ws.xxx.com/xxx/model/common">Petier< /ns3:lastname>
    <ns4:bday xmlns:ns4="http://ws.xxx.com/xxx/model/common">19310227< /ns4:bday>
    <ns5:rank xmlns:ns5="http://ws.xxx.com/xxx/model/common">1< /ns5:rank>
< /customer>

I tried to remove the attributes but with no luck.. 我试图删除属性,但没有运气。

Any idea? 任何想法?

You should remove the existing namespace definition attribute (if there is any) and add a new one: 您应该删除现有的名称空间定义属性(如果有)并添加一个新的名称空间定义属性:

element.Add(new XAttribute(XNamespace.Xmlns + "ns1", "http://ws.xxx.com/xxx/model/common"));

By adding an explicit namespace attribute you will force the XElement to use it for serializing. 通过添加一个显式的命名空间属性,您将强制XElement使用它进行序列化。

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

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