从XML文件中读取特定的XML元素

Rub*_*uba 25 c# xml

我有以下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>
Run Code Online (Sandbox Code Playgroud)

我需要阅读与C#应用此文件,如果只category就是verb我要打印整个元素word.
我怎样才能做到这一点?

sca*_*tag 23

你可以使用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);
Run Code Online (Sandbox Code Playgroud)


akt*_*ton 7

你也可以使用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);
}
Run Code Online (Sandbox Code Playgroud)

  • 我不同意 XPath 是老式的,我非常喜欢 XPath (2认同)
  • 我的意思是..您可以随心所欲,但是与linq to xml相比,它是老式的。 (2认同)

rbt*_*ong 5

这就是我要做的(下面的代码已经过测试,下面提供了完整的源代码),首先创建一个具有公共属性的类

    class Word
    {
        public string Base { get; set; }
        public string Category { get; set; }
        public string Id { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

使用 XDocument 和 INPUT_DATA 进行加载以进行演示,并使用lexicon查找元素名称。。。

    XDocument doc = XDocument.Parse(INPUT_DATA);
    XElement lex = doc.Element("lexicon");
Run Code Online (Sandbox Code Playgroud)

确保有一个值并使用 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>();
    }
Run Code Online (Sandbox Code Playgroud)

where语句检查类别元素是否存在以及类别值不为空,然后再次检查它是否是动词。然后检查其他节点是否也存在。。。

linq 查询将返回一个 IEnumerable< Typename > 对象,因此我们可以调用 ToArray< Typename >() 将整个集合转换为我们想要的类型。

然后打印得到 . 。。

[Found]
 Id: E0006429
 Base: abandon
 Category: verb

[Found]
 Id: E0006524
 Base: abolish
 Category: verb
Run Code Online (Sandbox Code Playgroud)

完整来源

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>";

    }
}
Run Code Online (Sandbox Code Playgroud)