C# 使用XDocument、XMLDocument或XPath向现有元素添加父元素?

C# 使用XDocument、XMLDocument或XPath向现有元素添加父元素?,c#,.net,xml,C#,.net,Xml,对于以下问题,哪个API需要更少的代码,XMLDocument还是XDoc?除了XMLDocument、XDoc或XPath之外,还有其他更适合此任务的方法吗?(忽略性能和文件大小较小) 尝试将某些元素转换为列表中出现的节点的子元素,例如(缩进不重要,请忽略它) 名称1 LName1 电话 姓名2 LName2 电话2 名字3 LName3 电话3 我想把他们变成这样 <PersonList> <Person> <PersonDetailsList&

对于以下问题,哪个API需要更少的代码,XMLDocument还是XDoc?除了XMLDocument、XDoc或XPath之外,还有其他更适合此任务的方法吗?(忽略性能和文件大小较小)

尝试将某些元素转换为列表中出现的节点的子元素,例如(缩进不重要,请忽略它)


名称1
LName1
电话
姓名2
LName2
电话2
名字3
LName3
电话3
我想把他们变成这样

<PersonList>
  <Person>
    <PersonDetailsList>
      <Name>Name1</Name>
      <LName>LName1</LName>
      <Phone>Phone</Phone>
    </PersonDetailsList>
  </Person>
  <Person>
    <PersonDetailsList>
      <Name>Name2</Name>
      <LName>LName2</LName>
      <Phone>Phone2</Phone>
    </PersonDetailsList>
  </Person>
    <Person>
      <PersonDetailsList>
        <Name>Name3</Name>
        <LName>LName3</LName>
        <Phone>Phone3</Phone>
      </PersonDetailsList>
  </Person>
</PersonList>

名称1
LName1
电话
姓名2
LName2
电话2
名字3
LName3
电话3

Xml Linq工作得很好:

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

namespace ConsoleApplication7
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            List<XElement> people = doc.Descendants("Person").ToList();
            foreach (XElement person in people)
            {
                var children = person.Nodes().ToList();
                person.ReplaceWith(new XElement("Person", new object[] {
                    new XElement("PersonDetailsList", children)
                }));
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序7
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List people=doc.subscriptions(“Person”).ToList();
foreach(人与人之间的关系)
{
var children=person.Nodes().ToList();
person.replacetwith(新元素(“person”,新对象[]){
新XElement(“PersonDetailsList”,儿童)
}));
}
}
}
}

Xml Linq工作得很好:

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

namespace ConsoleApplication7
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            List<XElement> people = doc.Descendants("Person").ToList();
            foreach (XElement person in people)
            {
                var children = person.Nodes().ToList();
                person.ReplaceWith(new XElement("Person", new object[] {
                    new XElement("PersonDetailsList", children)
                }));
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序7
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List people=doc.subscriptions(“Person”).ToList();
foreach(人与人之间的关系)
{
var children=person.Nodes().ToList();
person.replacetwith(新元素(“person”,新对象[]){
新XElement(“PersonDetailsList”,儿童)
}));
}
}
}
}

仅添加父节点

var doc = XDocument.Parse(xml);
var elems = doc.Descendants("Person");
foreach (XElement elem in elems)
{
    elem.ReplaceWith(new XElement("PersonDetailsList", elem));
}

只需添加父节点

var doc = XDocument.Parse(xml);
var elems = doc.Descendants("Person");
foreach (XElement elem in elems)
{
    elem.ReplaceWith(new XElement("PersonDetailsList", elem));
}

如果没有某种价值体系的定义,“更好”对工程师来说是毫无意义的。也许你可以更具体一些,例如“性能更好”或“兼容性更好”。否则答案纯粹是意见问题。@JohnWu:是的,改成哪种方式会更短,忽略性能和要转换的大量内容。@JohnWu:实际上,对于XPath,这可能是一项微不足道的任务,需要的编程远远少于XDoc或XMLDocument方法。我个人可能会反序列化列表,将其映射到另一个列表,然后序列化它。这将产生最可读、最可维护的代码,IMO。但它可能不是最短的。@JohnWu:如果是直截了当的话,我一直在努力解决错误,XML不是我经常做的事情,而且很可能在未来几年内不会再做了,很乐意采用简单的直接方法,只适用于这个问题或任何类似的简单测试数据。如果没有定义一些值系统,“更好”对工程师来说是毫无意义的。也许你可以更具体一些,例如“性能更好”或“兼容性更好”。否则答案纯粹是意见问题。@JohnWu:是的,改成哪种方式会更短,忽略性能和要转换的大量内容。@JohnWu:实际上,对于XPath,这可能是一项微不足道的任务,需要的编程远远少于XDoc或XMLDocument方法。我个人可能会反序列化列表,将其映射到另一个列表,然后序列化它。这将产生最可读、最可维护的代码,IMO。但它可能不是最短的。@JohnWu:如果是直截了当的话,我一直在努力解决错误,XML不是我经常做的事情,而且很可能在未来几年内不会再做了,很乐意采用简单的直接方法,只适用于这个问题或类似问题中的简单测试数据。