在Textbox C#中显示从Combobox中选择的XML数据

时间:2017-03-13 11:01:09

标签: c# xml visual-studio

你好我正在学习C#,我试图在组合框中选择同一表格的文本框中的User1.xml中显示xml信息。

我已设法使用此代码使用名称标签填充组合框。 (这样可以选择用户名)

private void Form1_Load(object sender, EventArgs e)
{
   XmlDocument sFor = new XmlDocument();
   sFor.Load(Path.GetFullPath("User1.xml"));
   XmlNodeList SearchList = sFor.SelectNodes("employee/user/name");
   foreach (XmlNode Search in SearchList)
   {
      comboBox1.Items.Add(Search.InnerText);
   }

}

XML格式化

<employee>
    <user>
    <name>John Smith</name>
    <department>PAI</department>
    <manager>MD</manager>
    <hours>full-time</hours>
    <leave>940</leave>
    </user>
</employee>

如何在Combobox中选择后(使用linq或xmlreader或其他方式),在textBox2中显示信息?

谢谢。

2 个答案:

答案 0 :(得分:0)

如果这可以帮到你

XDocument doc = XDocument.Load(Path.GetFullPath("User1.xml"));

var rows = doc.Descendants("employee").Descendants("user").Select(el => new()
{
  department = el.Element("department").Value,
  manager = el.Element("manager").Value,
  hours = el.Element("hours").Value,
  leave = el.Element("leave").Value,
});

来自DataSet的OR

DataSet ds = new DataSet();
ds.ReadXmlSchema(new StreamReader("User1.xml"));
ds.ReadXml(new StreamReader("User1.xml"));

答案 1 :(得分:0)

我使用DataGridView做了类似的事情,寻找&#34; City&#34;。输入字段是Textbox1.Text。我希望这不是太过分了。这个功能归功于Stack的海报,告诉我如何做到这一点!我首先提取了特定数据,然后将其加载到List。最后,使List成为DGV的数据源。这可能适用于组合框......是吗?

<TaxTbl>
  <TaxSite>
    <Location>Axx</Location>
    <Address>xxx</Address>
    <City>aaa</City>
    <State> st</State>
    <Zip>xxx</Zip>
  </TaxSite>
  <TaxSite>
    <Location>Bxxx</Location>
    <Address>xxx</Address>
    <City>xxx</City>
    <State> st</State>
    <Zip>xxx</Zip>
  </TaxSite>
</TaxTbl>






 var xdoc = XDocument.Load("C:\\Users\\Harley\\desktop\\outfile.xml");



var NewDoc = new XDocument(new XElement("TaxTbl",
             from anEntry in xdoc.Element("TaxTbl").Elements("TaxSite")
             where anEntry.Element("City").Value.Contains(textBox1.Text)
             select anEntry));



var MyList =
                    (from bEntry in NewDoc.Descendants("TaxSite")
                     select new
                     {
                         Location = bEntry.Element("Location").Value,
                         Address = bEntry.Element("Address").Value,
                         City = bEntry.Element("City").Value,
                         State = bEntry.Element("State").Value,
                         Zip = bEntry.Element("Zip").Value
                     }).ToList();


 cityDGV.DataSource = MyList.ToList();