我的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>
有没有办法根据相同的节点值对其进行分组?
答案 0 :(得分:1)
我刚刚测试了以下代码,它与您的搜索结果相符。
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
}));
}
}
}
}
答案 1 :(得分:0)
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();
答案 2 :(得分:0)
对元素进行分组并构建一个新文档,将每个组放在一个新的<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
)
)
);
答案 3 :(得分:0)
我必须在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>
与其他答案类似。