[英]Reading specific XML elements from XML file
I have the following XML file我有以下 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>
I need to read this file with C# application, and if only the category
is verb
I want to print its entire element word
.我需要用 C# 应用程序读取这个文件,如果只有
category
是verb
我想打印它的整个元素word
。
How can I do that?我怎样才能做到这一点?
You could use linq to xml.您可以使用 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);
You could use an XPath , too.您也可以使用XPath 。 A bit old fashioned but still effective:
有点老式但仍然有效:
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);
}
This is how I would do it (the code below has been tested, full source provided below), begin by creating a class with common properties这就是我的做法(下面的代码已经过测试,下面提供了完整的源代码),首先创建一个具有公共属性的类
class Word
{
public string Base { get; set; }
public string Category { get; set; }
public string Id { get; set; }
}
load using XDocument with INPUT_DATA for demonstration purposes and find element name with lexicon .出于演示目的,使用带有 INPUT_DATA 的 XDocument 加载并使用lexicon查找元素名称。 .
. .
.
XDocument doc = XDocument.Parse(INPUT_DATA);
XElement lex = doc.Element("lexicon");
make sure there is a value and use linq to extract the word elements from it .确保有一个值并使用 linq 从中提取单词元素。 .
. .
.
Word[] catWords = null;
if (lex != null)
{
IEnumerable<XElement> 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<Word>();
}
The where
statement checks if the category element exists and that the category value is not null and then check it again that it is a verb. where
语句检查category 元素是否存在并且category 值不为空,然后再次检查它是否是动词。 Then check that the other nodes also exists .然后检查其他节点是否也存在。 .
. .
.
The linq query will return an IEnumerable< Typename > object, so we can call ToArray< Typename >() to cast the entire collection into the type we want. linq 查询将返回一个 IEnumerable<Typename> 对象,因此我们可以调用 ToArray<Typename>() 将整个集合转换为我们想要的类型。
Then print it to get .然后打印出来得到。 .
. .
.
[Found]
Id: E0006429
Base: abandon
Category: verb
[Found]
Id: E0006524
Base: abolish
Category: verb
Full Source :完整来源:
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<XElement> 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<Word>();
}
//print it
if (catWords != null)
{
Console.WriteLine("Words with <category> 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 =
@"<?xml version=""1.0""?>
<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>";
}
}
XDocument xdoc = XDocument.Load(path_to_xml);
var word = xdoc.Elements("word")
.SingleOrDefault(w => (string)w.Element("category") == "verb");
This query will return whole word XElement
.此查询将返回整个单词
XElement
。 If there is more than one word element with category verb
, than you will get an InvalidOperationException
.如果有多个具有类别
verb
词元素,那么您将得到一个InvalidOperationException
。 If there is no elements with category verb
, result will be null
.如果没有带有类别
verb
元素,则结果将为null
。
Alternatively, you can use XPath query via XPathSelectElements
method:或者,您可以通过
XPathSelectElements
方法使用XPath查询:
var document = XDocument.Parse(yourXmlAsString);
var words = document.XPathSelectElements("//word[./category[text() = 'verb']]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.