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="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#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何删除命名空间前缀.(C#)


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01