简体   繁体   English

C#从xml文件获取元素

[英]C# Getting elements from an xml file

I have an XML file formatted like this: 我有一个格式如下的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<Snippets>
  <Snippet name="abc">
    <SnippetCode>
      testcode1
    </SnippetCode>
  </Snippet>

  <Snippet name="xyz">
    <SnippetCode>      
     testcode2
    </SnippetCode>
  </Snippet>

  ...

</Snippets>

I have populated a listbox with the snippet name, and it works fine so far. 我已经用代码段名称填充了一个列表框,到目前为止,它仍然可以正常工作。 For example (I haven't added any real snippets yet btw), my listbox contains: 例如(我还没有添加任何真实的片段),我的列表框包含:

abc abc
xyz y
123 123

When I click on an item in the listbox, I want the snippet code of that item to be inserted into a textbox. 当我单击列表框中的项目时,我希望将该项目的代码段插入到文本框中。 Like if abc was clicked, testcode1 should be inserted into the textbox. 就像单击abc一样,应将testcode1插入文本框。 I used this code on the double click event: 我在双击事件上使用了以下代码:

        XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
        foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
        {
            if (listBox1.SelectedItem == xe.Attribute("name"))
            {
            textbox1.Text = xe.Element("SnippetCode").Value;
            }
        }

However, nothing gets inserted because it never finds the snippet code value. 但是,不会插入任何内容,因为它永远找不到片段代码值。 I added a MessageBox.Show("test"); 我添加了一个MessageBox.Show(“ test”); inside the if statement to check if it executes but it never does. 在if语句中检查它是否执行,但是从不执行。 The selected listbox item name and snippetname have the same text, so it's quite strange it isn't ever executing. 所选的列表框项目名称和代码段名称具有相同的文本,因此很奇怪,它从未执行过。

Does anyone know what's wrong with my code? 有人知道我的代码有什么问题吗? Also, does anyone know of a better idea to insert text in the document from the snippet element? 另外,没有人知道从snippet元素在文档中插入文本的更好的主意吗? This method isn't probably good as performance might be a problem for large XML files. 这种方法可能不太好,因为对于大型XML文件而言,性能可能是一个问题。

You're comparing the attribute itself with the value, instead of the attribute's value. 您正在将属性本身与值而不是属性值进行比较。

Additionally, I can't remember offhand what the type of ListBox.SelectedItem is, but if it's object then that will be doing a reference comparison instead of equality. 另外,我不记得有什么ListBox.SelectedItem的类型,但是如果它是object那么它将进行引用比较而不是相等。

string selected = (string) listBox1.SelectedItem;
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
{
    if (xe.Attribute("name").Value == selected)
    {
        textbox1.Text = xe.Element("SnippetCode").Value;
    }
}

Note that this will fail with an exception if there are any snippets without a "name" attribute. 请注意,如果存在任何不带“名称”属性的代码片段,则此操作将失败,并例外。 That's probably a good thing if every snippet is meant to have a name attribute - but if they're allowed not to, then using the explicit string conversion instead of the Value property is simple: 如果每个代码段都应具有name属性,那可能是一件好事-但如果不允许这样做,则使用显式字符串转换而不是Value属性很简单:

string selected = (string) listBox1.SelectedItem;
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
{
    if ((string) xe.Attribute("name") == selected)
    {
        textbox1.Text = xe.Element("SnippetCode").Value;
    }
}

Note that you can also do this through LINQ: 请注意,您也可以通过LINQ执行此操作:

string selected = (string) listBox1.SelectedItem;
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
string code = doc.Elements("Snippets")
                 .Elements("Snippet")
                 .Where(x => x.Attribute("name").Value == selected)
                 .Select(x => x.Element("SnippetCode").Value)
                 .FirstOrDefault();

if (code != null)
{
    textbox1.Text = code;
}

I figured out the problem, xe.Attribute("name") was returning name="abc" instead of just abc. 我发现了问题,xe.Attribute(“ name”)返回的是name =“ abc”而不是abc。 My bad for not realzing this just after making the above post. 我不好,只是在发表以上文章后才意识到这一点。

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

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