2015-09-03 41 views
-1

我通過查找XML文檔中使用C#

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(fullPath); 

XML文件加載XML文檔的重複值已經像(簡化IT):

<customers> 
    <customer> 
    <guid> 
    <customer> 
    <customer> 
    <guid> 
    <customer> 
<customers> 

但在現實DOC孤單許多嵌套客戶。如何使用guid子元素搜索並查找所有客戶元素,並使用重複的GUID值(guid元素中的文本)。

回答

1

您可以使用LINQ找到重複:

var dublicates = XDocument.Parse(xml) 
      .Descendants("customer") 
      .GroupBy(g => (string)g.Attribute("guid")) 
      .Where(g => g.Count() > 1) 
      .Select(g => g.Key);