简体   繁体   English

C#XML Linq指向/读取节点

[英]C# XML Linq Pointing to/Reading Node

I can't seem to point and read the correct information. 我似乎无法指出并阅读正确的信息。 I'm new to using Linq and have tried (after loading the document as XDocument and XElement) select, root.xelement, descendant, element, node etc. and haven't found the proper way of pointing to what I'm trying to target. 我是使用Linq的新手,并尝试过(在将文档加载为XDocument和XElement后)选择,root.xelement,后代,元素,节点等,但没有找到指向我要尝试​​的内容的正确方法目标。 I have an XML document that looks like this for now. 我现在有一个看起来像这样的XML文档。

<Contacts>
   <EntryName>
     <Name>NAME1</Name>
     <Email>EMAIL</Email>
     <EIL>1</EIL>
     <Notes>Notes</Notes>
   </EntryName>
</Contacts>

I need to pull up a list of all EntryNames and place them in listBox1. 我需要提取所有EntryName的列表,并将它们放在listBox1中。 When a user selects one, it gathers it takes the "listBox1.SelectedItem" and gather's the email address associated and places it in a textBox. 当用户选择一个时,它将收集“ listBox1.SelectedItem”并收集关联的电子邮件地址,并将其放置在textBox中。 "EntryName" during runtime is replaced by a textfield. 运行时期间的“ EntryName”被文本字段替换。 My most recent try was this: 我最近的尝试是这样的:

    var xml = XDocument.Load(apppath + @"\Contacts.clf");
    var entries = xml.Element("Contacts").Value.ToString();

        foreach (var entry in entries)
        {
            listBox1.Items.Add(entry.ToString());
        }

Which gets me nothing but characters at a time of the complete file due to the foreach function. 由于使用了foreach函数,因此在整个文件中一次只能输入字符,这让我一无所有。 What I'm looking for is in the listBox from Contacts: 我正在寻找的是联系人列表框中的内容:

EntryName
EntryName2
EntryName2...etc

and when selected (from say EntryName2) it pulls the email field and places it in a textbox. 然后(从“ EntryName2”说出来)被选中时,它将拉出电子邮件字段并将其放置在文本框中。 Please forgive and obvious or dumb mistake, very new to this. 请原谅和明显或愚蠢的错误,这是很新的。 Thanks. 谢谢。

Try this. 尝试这个。 I believe you were trying to query the Name elements in your XML document. 我相信您正在尝试查询XML文档中的Name元素。

var xml = XDocument.Load(apppath + @"\Contacts.clf");
var entries = from entryName in xml.Descendants("EntryName") select (string)entryName.Element("Name");

foreach (var entry in entries)
{
   listBox1.Items.Add(entry);
}

I wrote a quick example on how to achieve this 我写了一个简单的例子来说明如何实现这一目标

public partial class Form1 : Form
{
    XDocument doc;
    public Form1()
    {
        InitializeComponent();

        doc = XDocument.Load(apppath + @"\Contacts.clf");
        var entryNames = doc.Root.Elements("EntryName")
            .Select(elem => elem.Element("Name").Value ).ToArray();
        listBox1.Items.AddRange(entryNames);
    }

    private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        textBox1.Text = doc.Root.Elements("EntryName")
            .FirstOrDefault(node => node.Element("Name").Value == listBox1.SelectedItem.ToString())
            .Element("Email").Value;

    }
}

However That seems like too much trouble to find the Email. 但是,找到电子邮件似乎很麻烦。 I would, instead handle it like this: 我会这样处理:

public partial class Form1 : Form
{
    XDocument doc;
    public Form1()
    {
        InitializeComponent();
        String apppath = ".";
        doc = XDocument.Load(apppath + @"\Contacts.clf");
        var contacts = doc.Root.Elements("EntryName")
            .Select( elem =>
                new Contact { 
                    Name =  elem.Element("Name").Value,
                    Email = elem.Element("Email").Value,
                    EIL = elem.Element("EIL").Value,
                    Notes = elem.Element("Notes").Value
            }
        ).ToList();
        listBox1.DataSource = contacts;
        listBox1.DisplayMember = "Name";
    }

    private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        textBox1.Text = (listBox1.SelectedItem as Contact).Email;
    }        
}

public class Contact
{
    public String Name { get; set; }
    public String Email { get; set; }
    public String EIL { get; set; }
    public String Notes { get; set; }
}

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

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