Select the elements you want to check (for example, all sub-elements of all tasks) and group them by value:
xdoc.Root.Elements("task").SelectMany(t => t.Elements())
.GroupBy(e => e.Value)
.Select(g => new { Text = g.Key, Count = g.Count() })
Request syntax:
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()
};
With XPath:
var result = from e in xdoc.XPathSelectElements("//task/*")
group e by e.Value into g
select new {
Text = g.Key,
Count = g.Count()
};
For your xml sample, the result will be:
[
{ 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()
};