我有以下
XML文件
<lexicon> <word> <base>a</base> <category>determiner</category> <id>E0006419</id> </word> <word> <base>abandon</base> <category>verb</category> <id>E0006429</id> <ditransitive/> <transitive/> </word> <word> <base>abbey</base> <category>noun</category> <id>E0203496</id> </word> <word> <base>ability</base> <category>noun</category> <id>E0006490</id> </word> <word> <base>able</base> <category>adjective</category> <id>E0006510</id> <predicative/> <qualitative/> </word> <word> <base>abnormal</base> <category>adjective</category> <id>E0006517</id> <predicative/> <qualitative/> </word> <word> <base>abolish</base> <category>verb</category> <id>E0006524</id> <transitive/> </word> </lexicon>
我需要用C#应用程序读取这个文件,如果只有类别是动词,我想打印它的整个元素单词.
我怎样才能做到这一点?
解决方法
你可以使用linq到xml.
var xmlStr = File.ReadAllText("fileName.xml"); var str = XElement.Parse(xmlStr); var result = str.Elements("word"). Where(x => x.Element("category").Value.Equals("verb")).ToList(); Console.WriteLine(result);