簡體   English   中英

如何根據C#中的XML節點值對XML節點進行分組

[英]How to group XML nodes based on their values in C#

我的XML為:

<root>
    <element>
        <id>1</id>
        <group>first</group>
    </element>
    <element>
        <id>2</id>
        <group>second</group>
    </element>
    <element>
        <id>3</id>
        <group>first</group>
    </element>
</root> 

無論如何,我們可以使用相同的值將節點分組,如下所示:

<root>
    <groups name="first">
      <element>
        <id>1</id>
        <group>first</group>
      </element>
      <element>
        <id>3</id>
        <group>first</group>
    </element>
  </groups>
   <groups name="second"><element>
       <id>2</id>
        <group>second</group>
    </element>
  </groups>
</root>

有沒有一種方法可以基於相同的節點值對其進行分組?

我剛剛測試了下面的代碼,它與您的結果匹配。

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<root>" +
                    "<element>" +
                        "<id>1</id>" +
                        "<group>first</group>" +
                    "</element>" +
                    "<element>" +
                        "<id>2</id>" +
                        "<group>second</group>" +
                    "</element>" +
                    "<element>" +
                        "<id>3</id>" +
                        "<group>first</group>" +
                    "</element>" +
                "</root>";

            XDocument doc = XDocument.Parse(xml);

            var groups = doc.Descendants("element")
                .GroupBy(x => (string)x.Element("group"))
                .ToList();


            XElement newXml = new XElement("root");
            foreach(var group in groups)
            {
                newXml.Add(new XElement("groups", new object[] {
                    new XAttribute("name", group.Key),
                        group
                }));
            }

        }
    }
}

XPath方法

string val= "first";
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./Xml/YourXML.xml"));
string text = string.Empty;
XmlNodeList xnl = doc.SelectNodes("/root/groups ");
foreach (XmlNode node in xnl)
{
    text = node.Attributes["name"].InnerText;
    if (text == val)
    {
         XmlNodeList xnl = doc.SelectNodes(string.Format("/root/groups [@name='{0}']/element", val));
        foreach (XmlNode node2 in xnl )
        {
            text = text + "<br>" + node2["id"].InnerText;
            text = text + "<br>" + node2["group"].InnerText;
        }
    }
    Response.Write(text);
}

要么

var nodes = (from n in xml.Descendants("element").
             Where(r => r.Parent.Attribute("name").Value == "first")
             select new
             {
                  id = (string)n.Element("id").Value,
                  group = (string)n.Element("group").Value
             }).ToList();

對元素進行分組並建立一個新文檔,將每個組放置在新的<groups>元素中。

var newDoc = new XDocument(
    new XElement("root",
        from e in doc.Descendants("element")
        group e by (string)e.Element("group") into g
        select new XElement("groups",
            new XAttribute("name", g.Key),
            g
        )
    )
);

我不得不在VB中嘗試。 我的分組依據技能需要大量練習。

使用此測試數據

    Dim myXML As XElement
    myXML = <root>
                <element>
                    <id>1</id>
                    <group>first</group>
                </element>
                <element>
                    <id>2</id>
                    <group>second</group>
                </element>
                <element>
                    <id>3</id>
                    <group>first</group>
                </element>
            </root>

這似乎有效

    Dim newXML As XElement = <root></root>

    newXML.Add(From el In myXML.Elements
               Order By el.<group>.Value
               Group By gn = el.<group>.Value Into g = Group
               Select New XElement("Groups", New XAttribute("name", gn), g))

newXML =

<root>
  <Groups name="first">
    <element>
      <id>1</id>
      <group>first</group>
    </element>
    <element>
      <id>3</id>
      <group>first</group>
    </element>
  </Groups>
  <Groups name="second">
    <element>
      <id>2</id>
      <group>second</group>
    </element>
  </Groups>
</root>

與其他答案類似。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM