我想知道是否有一种方法可以检查XML文件中有多少相等的字符串。例如,这是XML文件:
<Root>
<task>
<sub1>test</sub1>
<sub2>hello</sub2>
<sub3>csharp</sub3>
</task>
<task>
<sub1>test2</sub1>
<sub2>hello2</sub2>
<sub3>csharp2</sub3>
</task>
<task>
<sub1>test3</sub1>
<sub2>hello3</sub2>
<sub3>csharp3</sub3>
</task>
<task>
<sub1>test</sub1>
<sub2>hello4</sub2>
<sub3>csharp4</sub3>
</task>
</Root>
正如您所见,node.Innertext = "test"
存在两次。我想知道我怎么算这个?我试过像
client["sub1"].InnerText.Count
但这会计算此字符串中的字符数。
建议赞赏:)
编辑:我使用XmlDocument
答案 0 :(得分:8)
选择要检查的元素(例如,所有任务的所有子元素)并按值对其进行分组:
xdoc.Root.Elements("task").SelectMany(t => t.Elements())
.GroupBy(e => e.Value)
.Select(g => new { Text = g.Key, Count = g.Count() })
查询语法:
var xdoc = XDocument.Load(path_to_xml);
var result = from t in xdoc.Root.Elements("task")
from e in t.Elements()
group e by e.Value into g
select new {
Text = g.Key,
Count = g.Count()
};
使用XPath:
var result = from e in xdoc.XPathSelectElements("//task/*")
group e by e.Value into g
select new {
Text = g.Key,
Count = g.Count()
};
对于您的样本,xml结果将为:
[
{ Text: "test", Count: 2 },
{ Text: "hello", Count: 1 },
{ Text: "csharp", Count: 1 },
{ Text: "test2", Count: 1 },
{ Text: "hello2", Count: 1 },
{ Text: "csharp2", Count: 1 },
{ Text: "test3", Count: 1 },
{ Text: "hello3", Count: 1 },
{ Text: "csharp3", Count: 1 },
{ Text: "hello4", Count: 1 },
{ Text: "csharp4", Count: 1 }
]
如果您只想获得多次出现的文字,可以按计数过滤结果:
result.Where(x => x.Count > 1)
XmlDocument
的相同查询:
var doc = new XmlDocument();
doc.Load(path_to_xml);
var result = from XmlNode n in doc.SelectNodes("//task/*")
group n by n.InnerText into g
select new {
Text = g.Key,
Count = g.Count()
};
答案 1 :(得分:1)
var dubs = XDocument.Parse(xml)
.Descendants("task")
.GroupBy(g => (string)g.Attribute("sub1"))
.Where(g => g.Count() > 1)
.Select(g => g.Key);