如何删除命名空间前缀.(C#) [英] How to remove namespace prefix. (C#)

查看:122
本文介绍了如何删除命名空间前缀.(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XElement,它是由如下所示的转换产生的.

I have an XElement, resulting from a transform that looks like the following.

<src:Person xmlns:src="http://www.palantir.za">
  <src:Name>Jenifer Harvey</src:Name>
  <src:BirthDate>1969-11-13</src:BirthDate>
  <src:IdentityNumber>6906678550017</src:IdentityNumber>
  <src:Sex>Male</src:Sex>
</src:Person> 

我想将此 XElement 转换为一个新的 XElement,该 XElement 具有src"作为默认命名空间,而不是上面的命名"命名空间.

I would like to transform this XElement into a new XElement that has 'src' as a default namespace rather than a 'named' namespace as above.

我意识到它们可能在语义上是相同的,我只是想保持存储内容的一致性.

I realise they are probably semantically identical, I just want to maintain consistency in what is being stored.

我想要以下.

<Person xmlns="http://www.palantir.za">
        <Name>Jenifer Harvey</Name>
        <BirthDate>1969-11-13</BirthDate>
        <IdentityNumber>6906678550017</IdentityNumber>
        <Sex>Male</Sex>
</Person> 

谢谢

问候

克雷格.

推荐答案

只需删除 xmlns:src 属性并添加一个新的 xmlns 属性:

Simply remove the xmlns:src attribute and add a new xmlns attribute:

XDocument xdoc = XDocument.Parse(
        "<src:Person xmlns:src="http://www.palantir.za">" +
        "  <src:Name>Jenifer Harvey</src:Name>" +
        "  <src:BirthDate>1969-11-13</src:BirthDate>" +
        "  <src:IdentityNumber>6906678550017</src:IdentityNumber>" +
        "  <src:Sex>Male</src:Sex>" +
        "</src:Person>");

xdoc.Root.Add(new XAttribute("xmlns", "http://www.palantir.za"));
xdoc.Root.Attributes(XNamespace.Xmlns + "scr").Remove();

这篇关于如何删除命名空间前缀.(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆