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

查看:170
本文介绍了如何删除空间前缀。 (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转换成具有SRC作为默认的命名空间一种新的XElement而除上述一个命名的命名空间。

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> 



感谢

Thanks

问候

克雷格

推荐答案

只需删除的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天全站免登陆