从xml中获取单词的所有定义

时间:2017-05-25 01:52:45

标签: c# xml wpf

我有以下XML

<entry_list version="1.0">
<entry id="sweet tooth">
  <ew>sweet tooth</ew>
  <hw>sweet tooth</hw>
  <fl>noun</fl>
  <def>
    <date>14th century</date>
    <dt>:a craving or fondness for sweet food</dt>
  </def>
</entry>
</entry_list>

我如何只展示“对甜食的渴望或喜爱”部分? XML放在文本框中,但我希望文本框只显示渴望的行。

此外,如果我搜索不太具体的内容,例如“计算机”,则有许多定义。我如何选择最早的定义?这是计算机的XML

   <entry_list version="1.0">
  <entry id="computer">
    <ew>computer</ew>
    <subj>CP#IS</subj>
    <hw>com*put*er</hw>
    <sound>
      <wav>comput06.wav</wav>
      <wpr>kum-!pyU-tur</wpr>
    </sound>
    <pr>kノ冦-ヒ・yテシ-tノ决</pr>
    <fl>noun</fl>
    <lb>often attributive</lb>
    <def>
      <date>1646</date>
      <dt>:one that <fw>computes</fw></dt>
      <sd>specifically</sd>
      <dt>:a programmable usually electronic device that can store, retrieve, and process data</dt>
    </def>
    <uro>
      <ure>com*put*er*dom</ure>
      <sound>
        <wav>comput07.wav</wav>
        <wpr>kum-!pyU-tur-dum</wpr>
      </sound>
      <pr>-dノ冦</pr>
      <fl>noun</fl>
    </uro>
    <uro>
      <ure>com*put*er*less</ure>
      <sound>
        <wav>comput08.wav</wav>
        <wpr>kum-!pyU-tur-lus</wpr>
      </sound>
      <pr>-lノ冱</pr>
      <fl>adjective</fl>
    </uro>
    <uro>
      <ure>com*put*er*like</ure>
      <sound>
        <wav>comput09.wav</wav>
        <wpr>kum-!pyU-tur-+lIk</wpr>
      </sound>
      <pr>-ヒ畦トォk</pr>
      <fl>adjective</fl>
    </uro>
  </entry>
  <entry id="computer science">
    <ew>computer science</ew>
    <subj>CP</subj>
    <hw>computer science</hw>
    <fl>noun</fl>
    <def>
      <date>1961</date>
      <dt>:a branch of science that deals with the theory of computation or the design of computers</dt>
    </def>
  </entry>
  <entry id="analog computer">
    <ew>analog computer</ew>
    <subj>CP</subj>
    <hw>analog computer</hw>
    <fl>noun</fl>
    <def>
      <date>1948</date>
      <dt>:a computer that operates with numbers represented by directly measurable quantities (as voltages or rotations) <dx>compare <dxt>digital computer</dxt><dxt>hybrid computer</dxt></dx></dt>
    </def>
  </entry>
  <entry id="digital computer">
    <ew>digital computer</ew>
    <subj>CP</subj>
    <hw>digital computer</hw>
    <fl>noun</fl>
    <def>
      <date>1947</date>
      <dt>:a computer that operates with numbers expressed directly as digits <dx>compare <dxt>analog computer</dxt><dxt>hybrid computer</dxt></dx></dt>
    </def>
  </entry>
  <entry id="home computer">
    <ew>home computer</ew>
    <subj>CP</subj>
    <hw>home computer</hw>
    <fl>noun</fl>
    <def>
      <date>1976</date>
      <dt>:a personal computer used in the home</dt>
    </def>
  </entry>
  <entry id="hybrid computer">
    <ew>hybrid computer</ew>
    <subj>CP</subj>
    <hw>hybrid computer</hw>
    <fl>noun</fl>
    <def>
      <date>1968</date>
      <dt>:a computer system consisting of a combination of analog and digital computer systems</dt>
    </def>
  </entry>
  <entry id="personal computer">
    <ew>personal computer</ew>
    <subj>CP</subj>
    <hw>personal computer</hw>
    <fl>noun</fl>
    <def>
      <date>1976</date>
      <dt>:a general-purpose computer equipped with a microprocessor and designed to run especially commercial software (as a word processor or Internet browser) for an individual user</dt>
    </def>
  </entry>
</entry_list>

2 个答案:

答案 0 :(得分:1)

一步一步

  1. 如何首先编写一个c#应用程序来读取xml?
  2. 选择所需的xml标记
  3. 显示出来。
  4. 在C#中读取xml可以在http://www.c-sharpcorner.com/article/reading-and-writing-xml-in-C-Sharp/找到(流式) 对于小型xml,更喜欢DOM(一次性全部读取),这样,它有点简单:http://csharp.net-tutorials.com/xml/reading-xml-with-the-xmldocument-class/

    选择xml标签,在第一种情况下可以是for循环中的switch语句;或者就像在DOM情况下选择节点和属性一样简单。

    在WPF中显示应该是最简单的。只需将此值传递给WPF。

    希望有所帮助,

    我应该在上面写评论,但随着公司合并,域名更改,我丢失了我的电子邮件地址。我没有足够的意见在SO中发表评论。所以回答这里。希望帮助你,也帮助我建立积分。

答案 1 :(得分:0)

尝试像这样的xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            string word = "computer";

            List<XElement> computers = doc.Descendants("entry").Where(x => (string)x.Attribute("id") == word).ToList();

            var definitions = computers.Select(x => new {
                word = (string)x.Attribute("id"),
                definitions = x.Elements("def").Select(y => new {
                    dts = y.Elements("dt").Select(z => (string)z).ToList()
                }).FirstOrDefault()
            }).ToList();
        }
    }
}