C# 从 XML 文件中读取特定的 XML 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14110212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading specific XML elements from XML file
提问by Ruba
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
.
How can I do that?
我需要阅读与C#应用此文件,如果仅category
是verb
我要打印其全部元素word
。
我怎样才能做到这一点?
采纳答案by scartag
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);
回答by Sergey Berezovskiy
XDocument xdoc = XDocument.Load(path_to_xml);
var word = xdoc.Elements("word")
.SingleOrDefault(w => (string)w.Element("category") == "verb");
This querywill return whole word XElement
. If there is more than one word element with category verb
, than you will get an InvalidOperationException
. If there is no elements with category verb
, result will be null
.
此查询将返回整个单词XElement
。如果具有类别的单词元素不止一个verb
,那么您将得到一个InvalidOperationException
. 如果没有带有类别的元素verb
,结果将为null
.
回答by k.m
Alternatively, you can use XPathquery via XPathSelectElements
method:
或者,您可以通过以下方法使用XPath查询XPathSelectElements
:
var document = XDocument.Parse(yourXmlAsString);
var words = document.XPathSelectElements("//word[./category[text() = 'verb']]");
回答by akton
You could use an XPath, too. A bit old fashioned but still effective:
您也可以使用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);
}
回答by rbtLong
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. Then check that the other nodes also exists . . .
该where
语句检查 category 元素是否存在以及 category 值是否为空,然后再次检查它是否是动词。然后检查其他节点是否也存在。. .
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>";
}
}