【问题标题】:Change XML root element name更改 XML 根元素名称
【发布时间】:2010-08-30 14:22:19
【问题描述】:

我将 XML 存储在字符串变量中:

<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>

在这里,我想将 XML 标记 &lt;ItemMasterList&gt; 更改为 &lt;Masterlist&gt;。我该怎么做?

【问题讨论】:

  • 您应该发布一些您正在使用的代码,因为这样做的方法不止一种。
  • + 1 表示问题。我敢打赌,这里不存在这样的问题。非常不寻常的场景。

标签: c#


【解决方案1】:

System.Xml.XmlDocument 和同一个命名空间中的相关类在这里将证明对您来说是无价的。

XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("MasterList");
docNew.AppendChild(newRoot);
newRoot.InnerXml = doc.DocumentElement.InnerXml;
String xml = docNew.OuterXml;

【讨论】:

  • 修改为包含一个示例。
  • 对于更一般的情况,您可能需要担心根元素的属性,对吧? foreach (XmlAttribute attNode in doc.DocumentElement.Attributes) { newRoot.Attributes.Append((XmlAttribute)docNew.ImportNode(attNode, true)); }
【解决方案2】:

我知道我有点晚了,但我只需要添加这个答案,因为似乎没有人知道这一点。

XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");    
doc.Root.Name = "MasterList";

返回以下内容:

<MasterList>
  <ItemMaster>
    <fpartno>xxx</fpartno>
    <frev>000</frev>
    <fac>Default</fac>
  </ItemMaster>
</MasterList>

【讨论】:

  • 更简单,完全重命名,保持其他所有内容不变。
【解决方案3】:

可以使用LINQ to XML解析XML字符串,创建一个新的根,并将原根的子元素和属性添加到新的根中:

XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");

XDocument result = new XDocument(
    new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));

【讨论】:

    【解决方案4】:

    使用XmlDocument 方式,您可以执行以下操作(并保持树完整):

    XmlDocument oldDoc = new XmlDocument();
    oldDoc.LoadXml("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
    XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");
    
    XmlDocument newDoc = new XmlDocument();
    XmlElement ele = newDoc.CreateElement("MasterList");
    ele.InnerXml = node.InnerXml;
    

    如果你现在使用ele.OuterXml,将会返回:(你只需要字符串,否则使用XmlDocument.AppendChild(ele),你将能够更多地使用XmlDocument对象)

    <MasterList>
      <ItemMaster>
         <fpartno>xxx</fpartno>
         <frev>000</frev>
         <fac>Default</fac>
      </ItemMaster>
    </MasterList>
    

    【讨论】:

      【解决方案5】:

      正如 Will A 所指出的,我们可以这样做,但是对于 InnerXml 等于 OuterXml 的情况,以下解决方案将起作用:

      // Create a new Xml doc object with root node as "NewRootNode" and 
      // copy the inner content from old doc object using the LastChild.
                          XmlDocument docNew = new XmlDocument();
                          XmlElement newRoot = docNew.CreateElement("NewRootNode");
                          docNew.AppendChild(newRoot);
      // The below line solves the InnerXml equals the OuterXml Problem
                          newRoot.InnerXml = oldDoc.LastChild.InnerXml;
                          string xmlText = docNew.OuterXml;
      

      【讨论】:

        猜你喜欢
        • 2017-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        相关资源
        最近更新 更多