如何在c# .net中使用LINQ对XML数据进行分组
本文关键字:数据 XML LINQ net | 更新日期: 2023-09-27 18:18:21
我想使用c# .net读取xml文件,并在LINQ的帮助下,我想根据INSTANCE和CINSTANCE值在xml中分组节点。我怎么能做到呢?这是我的源模式:
<XYZ>
<TYPE>A</TYPE>
<INSTANCE>1357599</INSTANCE>
<CHILD>DESCRIPTION</CHILD>
<CINSTANCE>PQR</CINSTANCE>
<CPOS>0000</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472422</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357599</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472427</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357599</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
<XYZ>
<TYPE>A</TYPE>
<INSTANCE>1357600</INSTANCE>
<CHILD>DESCRIPTION</CHILD>
<CINSTANCE>PQR</CINSTANCE>
<CPOS>0000</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472425</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357600</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472426</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357600</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
应该是我的输出:
<Group>
<XYZ>
<TYPE>A</TYPE>
<INSTANCE>1357599</INSTANCE>
<CHILD>DESCRIPTION</CHILD>
<CINSTANCE>PQR</CINSTANCE>
<CPOS>0000</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472422</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357599</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472427</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357599</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
</Group>
<Group>
<XYZ>
<TYPE>A</TYPE>
<INSTANCE>1357600</INSTANCE>
<CHILD>DESCRIPTION</CHILD>
<CINSTANCE>PQR</CINSTANCE>
<CPOS>0000</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472425</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357600</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
<XYZ>
<TYPE>GP</TYPE>
<INSTANCE>1472426</INSTANCE>
<CHILD>A</CHILD>
<CINSTANCE>1357600</CINSTANCE>
<CPOS>0010</CPOS>
</XYZ>
</Group>
我需要根据INSTANCE==CINSTANCE
对XYZ节点进行分组。有什么方法可以写一个逻辑吗?
您可能正在寻找类似于下面代码的内容,但是需要对您的格式做一个小的更改,它需要在Xml中有一个有效的父元素。
static void Main(string[] args)
{
string xml = @"<ELEMENTS><XYZ> <TYPE>A</TYPE> <INSTANCE>1357599</INSTANCE> <CHILD>DESCRIPTION</CHILD> <CINSTANCE>PQR</CINSTANCE> <CPOS>0000</CPOS> </XYZ> <XYZ> <TYPE>GP</TYPE> <INSTANCE>1472422</INSTANCE> <CHILD>A</CHILD> <CINSTANCE>1357599</CINSTANCE> <CPOS>0010</CPOS> </XYZ> <XYZ> <TYPE>GP</TYPE> <INSTANCE>1472427</INSTANCE> <CHILD>A</CHILD> <CINSTANCE>1357599</CINSTANCE> <CPOS>0010</CPOS> </XYZ> <XYZ> <TYPE>A</TYPE> <INSTANCE>1357600</INSTANCE> <CHILD>DESCRIPTION</CHILD> <CINSTANCE>PQR</CINSTANCE> <CPOS>0000</CPOS> </XYZ> <XYZ> <TYPE>GP</TYPE> <INSTANCE>1472425</INSTANCE> <CHILD>A</CHILD> <CINSTANCE>1357600</CINSTANCE> <CPOS>0010</CPOS> </XYZ> <XYZ> <TYPE>GP</TYPE> <INSTANCE>1472426</INSTANCE> <CHILD>A</CHILD> <CINSTANCE>1357600</CINSTANCE> <CPOS>0010</CPOS> </XYZ></ELEMENTS>";
Console.WriteLine(GetGroups(xml).ToString());
}
private static XDocument GetGroups(string xml)
{
XDocument xyzElementsDocument = XDocument.Parse(xml);
var results = from xyzElement1 in xyzElementsDocument.Descendants("XYZ")
join xyzElement2 in xyzElementsDocument.Descendants("XYZ")
on (string)xyzElement1.Element("INSTANCE") equals (string)xyzElement2.Element("CINSTANCE") into joinedElements
from joinedElement in joinedElements.DefaultIfEmpty()
group xyzElement1 by joinedElement != null into groupedElements
select new { HasCInstance = groupedElements.Key, Elements = groupedElements.Distinct() };
XDocument groupDocument = new XDocument();
groupDocument.Add(new XElement("GROUPS"));
foreach (var result in results)
{
XElement groupElement = new XElement("GROUP");
groupElement.Add(result.Elements);
groupDocument.Root.Add(groupElement);
}
return groupDocument;
}