替换XDocument中的根节点 [英] Replace the root node in XDocument

查看:77
本文介绍了替换XDocument中的根节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以替换XDocument的根元素?

我目前有一个看起来像这样的文档:

<ArrayOfCompletedInvoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CompletedInvoice>
    <Invoice>1</Invoice>
    ...
  </CompletedInvoice>
</ArrayOfCompletedInvoice>

我希望它看起来像:

<ns0:CompletedInvoices xmlns:ns0="http://myNamespace">
  <CompletedInvoice>
    <Invoice>1</Invoice>
    ...
  </CompletedInvoice>
</ns0:CompletedInvoices>

我知道我可以用XDocument.Root.Name替换名称,但是我一直在寻找是否有办法替换整个元素?

解决方案

最简单的选择是使用新的根目录创建一个新的XDocument,并从原始文档中复制第一级XElement. /p>

Is it possible to replace the root element of an XDocument?

I currently have a document that looks like:

<ArrayOfCompletedInvoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CompletedInvoice>
    <Invoice>1</Invoice>
    ...
  </CompletedInvoice>
</ArrayOfCompletedInvoice>

I want it to look like:

<ns0:CompletedInvoices xmlns:ns0="http://myNamespace">
  <CompletedInvoice>
    <Invoice>1</Invoice>
    ...
  </CompletedInvoice>
</ns0:CompletedInvoices>

I know I can replace the name with XDocument.Root.Name, but I am stuck seeing if there's a way to replace the entire element?

解决方案

The easiest option would be to create a new XDocument with a new root and simply copy over the first level XElements from your original document.

这篇关于替换XDocument中的根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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