0

我想阅读以下 xml 并在组合框中填充知识标签,在文本框中填充itemname标记,以及在组合框中填充其余部分。任何代码示例都会非常有帮助。

<?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>

如果我试试这个:

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()); 
     } 
}

它抛出一个错误。

4

1 回答 1

2

学会爱上 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);
        }
于 2012-04-24T21:35:48.107 回答