Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于属性值(c#)合并XML节点_C#_Asp.net_Xml - Fatal编程技术网

基于属性值(c#)合并XML节点

基于属性值(c#)合并XML节点,c#,asp.net,xml,C#,Asp.net,Xml,我需要根据属性(id)的值合并XMLnodes元素,并使用C将结果生成一个列表# 这是原始XML文件: <profiles> <profile id="1" > <name>John</name> <age>23</age> <sex>male</sex> </profile > <profi

我需要根据属性(id)的值合并XMLnodes元素,并使用C将结果生成一个列表#

这是原始XML文件:

   <profiles>
    <profile id="1" >
          <name>John</name>
          <age>23</age>
          <sex>male</sex>
    </profile >

    <profile id="2" >
          <name>Mark</name>
          <age>60</age>
    </profile >
    <profile id="2" >
          <sex>male</sex>
    </profile >
 </profiles>

你试过什么?请向我们展示您的代码我是XML的初学者,我不知道我建议您从这里开始阅读:如果您试图做一些事情,但并不完全是您想要的,那么您可以在这里添加您的C代码,我们将帮助您。但现在你要求我们做你的工作。从这里开始:
XDocument.Load(String)
好的,我做了一些工作,但工作不正常,问题已更新
       <profiles>
    <profile id="1" >
          <name>John</name>
          <age>23</age>
          <sex>male</sex>
    </profile >

    <profile id="2" >
          <name>Mark</name>
          <age>60</age>        
          <sex>male</sex>
    </profile >
 </profiles>
var employee = from emp in fileDoc.Descendants("profile")
               group emp by (string) emp.Attribute("id")
               into emps 
               select new Data
               {
                    ID =emps.Last().Attribute("id") != null ? emps.Last().Attribute("id").Value: "",
                    ProfileName =emps.Elements("name") != null? emps.Elements("name").Last().Value: "",
                    Sex=emps.Elements("sex") != null? emps.Elements("sex").Last().Value: ""
               };
var xDoc = XDocument.Parse(xml); //or XDocument.Load(fileName)

var newXDoc = new XElement("profiles",
                                xDoc.Descendants("profile")
                                .GroupBy(p => p.Attribute("id").Value)
                                .Select(p => new XElement("profile", 
                                                    new XAttribute(p.First().Attribute("id")), 
                                                    p.Elements())));

string newxml = newXDoc.ToString();