简体   繁体   English

.NET和读取xml文件

[英].NET and reading xml file

I want to read following xml and populate knowledge tags in a combo box , itemname tag in a textbox and the rest in combo boxes as well. 我想阅读以下xml并在组合框中填充知识标签,在文本框中填充itemname标签,并在组合框中填充其余部分。 Any code sample will be very helpful. 任何代码示例都非常有用。

<?xml version="1.0" encoding="UTF-8"?>
<swobs>
    <item>
          <knowledge>1</knowledge>
          <knowledge>2</knowledge>
          <knowledge>3</knowledge>
          <knowledge>4</knowledge>
          <itemname>INS Gator Operator</itemname>
          <knowhow>1</knowhow>
          <knowhow>2</knowhow>
          <knowhow>3</knowhow>
          <knowhow>4</knowhow>
          <supervisor>1</supervisor>      
          <supervisor>2</supervisor>      
          <supervisor>3</supervisor>      
          <supervisor>4</supervisor>  
       </item>              
</swobs>

If I try this: 如果我试试这个:

public void LoadXML() { 
    string myXMLfile = Server.MapPath("~/swobs.xml"); 
    DataSet dssowbs = new DataSet(); 
    try 
    { 
          dssowbs.ReadXml(myXMLfile); 
          DropDownList1.DataSource = dssowbs;
          DropDownList1.DataValueField = "knowledge"; 
          DropDownList1.DataBind(); 
     } 
     catch (Exception ex) 
     { 
          Response.Write(ex.ToString()); 
     } 
}

It throws an error. 它抛出一个错误。

Learn to love LINQ... this is how easy it is: 学会爱LINQ ...这是多么容易:

private void LoadData()
        {
            var allData = XElement.Load("yourdatafile.xml");
            this.comboKnowledge.ItemsSource = allData.Descendants("knowledge").Select(x => x.Value);
            this.textItemName.Text = allData.Descendants("itemname").Select(x => x.Value).SingleOrDefault();
            this.comboKnowHow.ItemsSource = allData.Descendants("knowhow").Select(x => x.Value);
            this.comboSupervisor.ItemsSource = allData.Descendants("supervisor").Select(x => x.Value);
        }

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

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