简体   繁体   English

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

[英]Using XDocument to load specific tags into a listbox

This is my first time working with XML files and I could use a little help. 这是我第一次使用XML文件,我需要一点帮助。 I'm also very new to C#. 我对C#也很陌生。 I have created an XML file that contains different word sets based on themes. 我创建了一个XML文件,其中包含基于主题的不同单词集。 What I would like to do is load all the words from a specific tag only. 我想做的是仅加载来自特定标签的所有单词。 However, I do not get an error, it just skips over the code that's supposed to add it to the list box. 但是,我没有收到错误,只是跳过了应该添加到列表框中的代码。

Here's a sample of my XML file: 这是我的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>

I'd like to take all the tags labeled and place them in a listbox. 我想将所有标记为标签的标签放置在列表框中。 Here is the code I have so far: 这是我到目前为止的代码:

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;

    }

Any help is appreciated! 任何帮助表示赞赏!

You skipped a level (fantasy and common). 您跳过了一个级别(幻想和普通)。

But if you are already using XDocument why don't you take advantage of LINQ? 但是,如果您已经在使用XDocument为什么不利用LINQ?

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

You can also use XPath 您也可以使用XPath

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM