从XML文件中读取特定的XML元素
我有以下XML文件
a determiner E0006419 abandon verb E0006429 abbey noun E0203496 ability noun E0006490 able adjective E0006510 abnormal adjective E0006517 abolish verb E0006524
我需要用C#应用程序读取这个文件,如果只有category
是verb
我想打印它的整个元素word
。
我怎样才能做到这一点?
你可以使用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);
你也可以使用XPath 。 有点老式但仍然有效:
using System.Xml; ... XmlDocument xmlDocument; xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); foreach (XmlElement xmlElement in xmlDocument.DocumentElement.SelectNodes("word[category='verb']")) { Console.Out.WriteLine(xmlElement.OuterXml); }
XDocument xdoc = XDocument.Load(path_to_xml); var word = xdoc.Elements("word") .SingleOrDefault(w => (string)w.Element("category") == "verb");
此查询将返回整个单词XElement
。 如果有多个带有类别verb
单词元素,则会出现InvalidOperationException
。 如果没有带类别verb
元素,则结果将为null
。
我就是这样做的(下面的代码已经过测试,下面提供了完整的源代码),首先创建一个具有公共属性的类
class Word { public string Base { get; set; } public string Category { get; set; } public string Id { get; set; } }
使用带有INPUT_DATA的XDocument进行加载以进行演示,并使用lexicon查找元素名称。 。 。
XDocument doc = XDocument.Parse(INPUT_DATA); XElement lex = doc.Element("lexicon");
确保有一个值并使用linq从中提取单词元素。 。 。
Word[] catWords = null; if (lex != null) { IEnumerable words = lex.Elements("word"); catWords = (from itm in words where itm.Element("category") != null && itm.Element("category").Value == "verb" && itm.Element("id") != null && itm.Element("base") != null select new Word() { Base = itm.Element("base").Value, Category = itm.Element("category").Value, Id = itm.Element("id").Value, }).ToArray(); }
where
语句检查category元素是否存在以及类别值是否为null,然后再次检查它是否为动词。 然后检查其他节点是否也存在。 。 。
linq查询将返回一个IEnumerable
然后将其打印出来。 。 。
[Found] Id: E0006429 Base: abandon Category: verb [Found] Id: E0006524 Base: abolish Category: verb
完整来源 :
using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace test { class Program { class Word { public string Base { get; set; } public string Category { get; set; } public string Id { get; set; } } static void Main(string[] args) { XDocument doc = XDocument.Parse(INPUT_DATA); XElement lex = doc.Element("lexicon"); Word[] catWords = null; if (lex != null) { IEnumerable words = lex.Elements("word"); catWords = (from itm in words where itm.Element("category") != null && itm.Element("category").Value == "verb" && itm.Element("id") != null && itm.Element("base") != null select new Word() { Base = itm.Element("base").Value, Category = itm.Element("category").Value, Id = itm.Element("id").Value, }).ToArray(); } //print it if (catWords != null) { Console.WriteLine("Words with and value verb:\n"); foreach (Word itm in catWords) Console.WriteLine("[Found]\n Id: {0}\n Base: {1}\n Category: {2}\n", itm.Id, itm.Base, itm.Category); } } const string INPUT_DATA = @" a determiner E0006419 abandon verb E0006429 abbey noun E0203496 ability noun E0006490 able adjective E0006510 abnormal adjective E0006517 abolish verb E0006524 "; } }
或者,您可以通过XPathSelectElements
方法使用XPath查询:
var document = XDocument.Parse(yourXmlAsString); var words = document.XPathSelectElements("//word[./category[text() = 'verb']]");