How to remove namespace prefix. (C#)(如何删除命名空间前缀.(C#))
问题描述
我有一个 XElement,它是由如下所示的转换产生的.
I have an XElement, resulting from a transform that looks like the following.
<src:Person xmlns:src="aHR0cDovL3d3dy5wYWxhbnRpci56YQ==">
<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="aHR0cDovL3d3dy5wYWxhbnRpci56YQ==">" +
" <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#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何删除命名空间前缀.(C#)


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04