在文本框C#中显示从ComboBox选择的XML数据



您好,我正在学习C#,并且我正在尝试从user1.xml中显示XML信息,以在ComboBox中选择同一形式的文本框中。

我已经使用此代码设法用名称标签填充了ComboBox。(以便可以选择用户名)

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?

中显示信息

谢谢。

我在datagridview寻找"城市"时做了类似的事情。输入字段是TextBox1.Text。我希望这不是过分的。荣誉归功于Stack的海报,向我展示了如何做到这一点!我首先提取特定数据,然后将其加载到列表中。最后,将列表列出了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();

如果这可以帮助您

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 ds = new DataSet();
ds.ReadXmlSchema(new StreamReader("User1.xml"));
ds.ReadXml(new StreamReader("User1.xml"));

相关内容

  • 没有找到相关文章

最新更新