C# 在xml c中查找属性值计数

C# 在xml c中查找属性值计数,c#,xml,C#,Xml,例如 XML文件 <root> <record query="semantic web mining" title="Semantic Web Mining" Url="www.cs.purdue.edu" Content="Agenda. ○ Semantic Web Mining aim. ○ Web Mining overview. ○ Semantic &#xA;Web overview. ○ Ontology Building. ○ Learning an

例如

XML文件

<root>
  <record query="semantic web mining" title="Semantic Web Mining" Url="www.cs.purdue.edu" Content="Agenda. ○ Semantic Web Mining aim. ○ Web Mining overview. ○ Semantic &#xA;Web overview. ○ Ontology Building. ○ Learning an Ontology using WM ..." ST="3/3/2014 2:21:06 PM" ET="3/3/2014 2:21:09 PM" />     
  <record query="apple computer" title="Fry's Electronics | Apple Computers" Url="www.frys.com" Content="Shop Frys.com for apple computers. ... Apple MacBook Pro 13.3 MD101LL/A. &#xA;ships_free. Apple 13.3 MB Pro 4GB Memory 500GB Hard Drive, M. Price:." ST="3/3/2014 2:28:23 PM" ET="3/3/2014 2:28:37 PM" />
  <record query="apple computer" title="What is Macintosh Computer? Webopedia" Url="www.webopedia.com" Content="A popular model of computer made by Apple Computer." ST="3/4/2015 3:13:07 PM" ET="3/4/2015 3:13:23 PM" />
  <record query="apple computer" title="Mac reviews | Mac deals, ratings, user reviews and prices - PC Advisor" Url="www.pcadvisor.co.uk" Content="The giant 27-inch iMac makes a great computer or movie screen player. .... Billed &#xA;as the world's most energy efficient desktop computer, the Apple Mac mini ..." ST="3/4/2015 3:13:24 PM" ET="3/4/2015 3:13:41 PM" />
  <record query="apple computer" title="Mac reviews | Mac deals, ratings, user reviews and prices - PC Advisor" Url="www.pcadvisor.co.uk" Content="The giant 27-inch iMac makes a great computer or movie screen player. .... Billed &#xA;as the world's most energy efficient desktop computer, the Apple Mac mini ..." ST="3/4/2015 3:13:24 PM" ET="3/4/2015 3:13:59 PM" />    
  <record query="apple fruit" title="Apple fruit nutrition facts and health benefits - Nutrition and You" Url="www.nutrition-and-you.com" Content="Delicious and crunchy apple fruit is one of the popular fruits containing an &#xA;impressive list of antioxidants and essential nutrients required for good health…" ST="3/3/2014 2:30:37 PM" ET="3/3/2014 2:30:41 PM" />
  <record query="apple fruit" title="Apple Facts - Apples and More - University of Illinois Extension" Url="urbanext.illinois.edu" Content="The crabapple is the only apple native to North America. Apples come in all ... &#xA;Apple trees take four to five years to produce their first fruit. Most apples are still ..." ST="3/3/2014 3:16:52 PM" ET="3/3/2014 3:17:04 PM" />
  <record query="apple fruit health" title="15 health benefits of eating apples | Health After 40 | Get Healthy ..." Url="www.besthealthmag.ca" Content="Many of us forget that sometimes, the simplest answers are the best. Better health &#xA;could be as easy as reaching for the fruit bowl for some apples next time you ..." ST="3/3/2014 3:13:29 PM" ET="3/3/2014 3:15:50 PM" />
  <record query="semantic web mining" title="The Semantic Web and Applications - Dr. Soon Ae Chun - CUNY" Url="cis.csi.cuny.edu" Content="The techniques to realize and/or utilize the Semantic Web are discussed in this &#xA;track. ... for Semantic Web applications; 2) utilizing ontologies for data &#xA;management, ... Querying the Semantic Web; Semantic Web mining; Question &#xA;Answering ..." ST="3/4/2015 3:09:46 PM" ET="3/4/2015 3:10:23 PM" />
</root>
如果记录节点值的Url属性计数=www.pcadvisor.co.uk,则应返回2

//Replace below xml with your complete xml.

string xml = @"<root>
  <record query=""semantic web mining"" title=""Semantic Web Mining"" Url=""www.frys.com"" Content=""Agenda. ○ Semantic Web Mining aim. ○ Web Mining overview. ○ Semantic &#xA;Web overview. ○ Ontology Building. ○ Learning an Ontology using WM ..."" ST=""3/3/2014 2:21:06 PM"" ET=""3/3/2014 2:21:09 PM"" />     
  <record query=""apple computer"" title=""Fry's Electronics | Apple Computers"" Url=""www.frys.com"" Content=""Shop Frys.com for apple computers. ... Apple MacBook Pro 13.3 MD101LL/A. &#xA;ships_free. Apple 13.3 MB Pro 4GB Memory 500GB Hard Drive, M. Price:."" ST=""3/3/2014 2:28:23 PM"" ET=""3/3/2014 2:28:37 PM"" /></root>";

            XElement element = XElement.Parse(xml);
            int count = element.Elements("record").Where(x => x.Attribute("Url").Value.Equals("www.pcadvisor.co.uk", StringComparison.InvariantCultureIgnoreCase)).Count();
            Console.WriteLine("count is:" + count.ToString());
您可以使用以下代码:

XDocument.Parse(Xml)
         .Element("root")
         .Descendants("record")
         .Count(r => r.Attribute("Url").Value == "www.pcadvisor.co.uk")

这里我使用Count方法的重载,它接受一个谓词。

Welcome。注意改进你的问题。由于这一点,很难理解您要求的是什么,以及到目前为止您已经尝试了什么。QuickTip:阅读XPath和属性选择器,直到您遵循@mins-tip。