重命名xml后代

时间:2017-09-05 12:08:42

标签: c# xml linq

我试图

  1. 加载外部xml

  2. 仅选择" oldName"节点

  3. 更改" oldName"节点到" newName"节点

  4. 创建并保存新的xml文件

  5. 以下代码不起作用,它只会创建"项目"节点

    // Load xml
    XElement doc = XElement.Load("http://.../file.xml");
    var nodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("oldName"));
    
    foreach (var element in nodes)
    {
        if (element.Name.LocalName.Equals("oldName"))
        {
            element.Name = "newName";
        }
    }
    
    XElement newDoc = new XElement("item", nodes);
    
    // Save xml
    newDoc.Save("C:/newFile.xml");
    

    任何帮助都将不胜感激。

    编辑:选择" oldName"节点工作,但我也需要重命名它们。

    // Load xml
    XElement doc = XElement.Load("http://.../file.xml");
    var nodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("oldName"));
    
    XElement newDoc = new XElement("item", nodes);
    
    // Save xml
    newDoc.Save("C:/newFile.xml");
    

    编辑:添加了文件示例

    file.xml

    <ignoreThisElement>
       <oldName>
          ...
       </oldName>
    </ignoreThisElement>
    

    newFile.xml

    <newName>
       ...
    </newName>
    

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情

// Load the xml
var doc = XDocument.Load("http://.../file.xml"); 

// loop thru the element an d search for a specific node and change the name
foreach (var element in doc.Descendants())
{
    if (element.Name.LocalName.StartsWith("oldName"))
    {
        element.Name = "newName";
    }
}

// Save xml
doc.Save("C:/newFile.xml");

答案 1 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XElement doc = XElement.Load(FILENAME);
            List<XElement> nodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("oldName")).ToList();

            for(int index = nodes.Count() - 1; index >= 0; index--)
            {
                XElement node = nodes[index];
                node.ReplaceWith(new XElement("newName", node.Descendants()));
            }
        }
    }
}

答案 2 :(得分:0)

您的代码只会创建一个空的&#34;项目&#34;节点是因为节点&#39; var不再包含具有旧名称的元素。您已将所有元素更改为新变量,但linq查询仍引用旧名称。

// Load xml
XElement doc = XElement.Load("http://.../file.xml");

//using linq to get all nodes with name 'oldName'
var nodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("oldName"));

//you rename the nodes here, so the linq query no longer finds nodes with 'oldName'
foreach (var element in nodes)
{
 if (element.Name.LocalName.Equals("oldName"))
 {
    element.Name = "newName";
 }
}

//result is 'items' node with no children
XElement newDoc = new XElement("item", nodes);

// Save xml
newDoc.Save("C:/newFile.xml");

解决方案是首先提取所需的节点,然后将它们移动到newDoc元素。此后,您可以重命名节点:

        // Load xml
        XElement doc = XElement.Load("http://.../file.xml");

        var nodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("oldName"));

        //move the extracted old nodes to your newDoc first
        XElement newDoc = new XElement("item", nodes);

         //now rename
        foreach (var element in newDoc.Descendants())
        {
            if (element.Name.LocalName.StartsWith("oldName"))
            {
                element.Name = "newName";
            }
        }

        // Save xml
        newDoc.Save("C:/newFile.xml");

或者,您可以在重命名后重新查询新节点名称的doc变量:

// Load xml
XElement doc = XElement.Load("http://.../file.xml");

//using linq to get all nodes with name 'oldName'
var nodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("oldName"));

foreach (var element in nodes)
{
  if (element.Name.LocalName.Equals("oldName"))
  {
   element.Name = "newName";
  }
}

//get all newName nodes
var newNodes = doc.Descendants().Where(element => element.Name.LocalName.Equals("newName"));

XElement newDoc = new XElement("item", newNodes);

// Save xml
newDoc.Save("C:/newFile.xml");
相关问题