使用XDocument将特定标记加载到列表框中

时间:2017-08-12 21:04:25

标签: c# xml listbox linq-to-xml xelement

这是我第一次使用XML文件,我可以使用一些帮助。我也是C#的新手。我创建了一个XML文件,其中包含基于主题的不同单词集。我想做的是只加载特定标签中的所有单词。但是,我没有收到错误,只是跳过了应该将它添加到列表框的代码。

以下是我的XML文件示例:

<?xml version="1.0"?>
<theme>
        <fantasy>
            <word> 
                <nn>wizard</nn>
                <nns>wizards</nns>
                <type>person</type>
            </word>
            <word> 
                <nn>wand</nn>
                <nns>wands</nns>
                <type>thing</type>
            </word>
            <word> 
                <vb>conjure</vb>
                <vbg>conjuring</vbg>
                <vbd>conjured</vbd>
            </word>
            <word> 
                <nnp>Merlin</nnp>
                <type>person</type>
            </word>
    </fantasy>
    <common>
            <word> 
                <vb>run</vb>
                <vbg>running</vbg>
                <vbd>ran</vbd>
            </word> 
            <word> 
                <nnp>Jeremy</nnp>
                <type>person</type>
            </word>
            <word> 
                <nnp>Dylan</nnp>
                <type>person</type>
            </word>
            <word> 
                <nnp>Darlene</nnp>
                <type>person</type>
            </word>     
            <word> 
                <nnp>Chelsea</nnp>
                <type>person</type>
            </word>
            <word> 
                <jj>beautiful</jj>
                <rb>beautifully</rb>
            </word> 
            <word> 
                <jj>ugly</jj>
            </word> 
            <word> 
                <jj>disgusting</jj>
                <vbn>disgusted</vbn>
                <rb>disgustingly</rb>
                <nn>disgust</nn>                
            </word> 
        </common>
</theme>

我想标记所有标签,并将它们放在列表框中。这是我到目前为止的代码:

public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void menuOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.ShowDialog();
            DocHandler.fileName = open.FileName;
            DocHandler.doc = XDocument.Load(DocHandler.fileName);
            txtText.Text = DocHandler.doc.ToString();

            GetElements();

        }

        public void GetElements()
        {
            foreach (XElement element in DocHandler.doc.Root.Elements())
            {
                if (element.Name.LocalName.Contains("word"))
                {
                    foreach (XElement subelement in element.Elements())
                    {
                        if (subelement.Name.LocalName.Contains("vb"))
                        {
                            listElements.Items.Add(subelement.Value.ToString());
                        }
                    }
                }
            }
        }
    }
    public class DocHandler
    {
        public static string fileName { get; set; }
        public static XDocument doc;

    }

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

你跳过了一个级别(幻想和普通)。

但如果您已经在使用XDocument,为什么不利用LINQ?

var words = DocHandler.doc.Descendants("vb").Select(element => element.Value);

答案 1 :(得分:0)

您也可以使用XPath

var items = DocHandler.doc.Document.XPathSelectElements("//word/vb")
                          .Select(x => x.Value)
                          .ToList();