2015-09-21 128 views
0

我的Xml文件看起來像這樣。將Xml加載到DropDown中

<?xml version="1.0" encoding="utf-8" ?> 
<Test1> 
    <Product Name="Test1" Value="10"> 
    <Catagory> 
     <CatagoryType Value="20">AAA</CatagoryType> 
     <CatagoryType Value="22">BBB</CatagoryType> 
     <CatagoryType Value="23">CCC</CatagoryType> 
     <CatagoryType Value="25">DDD</CatagoryType> 
    </Catagory> 
    <Type> 
     <TypeName Value="11">111</TypeName> 
     <TypeName Value="12">222</TypeName> 
     <TypeName Value="13">333</TypeName> 
     <TypeName Value="14">444</TypeName> 
    </Type> 
    <Location> 
     <Area Value="0">Inside</Area> 
     <Area Value="1">OutSide</Area> 
     <Area Value="2">NoWhere</Area> 
    </Location> 
    </Product> 
    <Product Name="Test2" Value="10"> 
    <Catagory> 
     <CatagoryType Value="20">EEE</CatagoryType> 
     <CatagoryType Value="22">FFF</CatagoryType> 
     <CatagoryType Value="23">GGG</CatagoryType> 
     <CatagoryType Value="25">HHH</CatagoryType> 
    </Catagory> 
    <Type> 
     <TypeName Value="11">555</TypeName> 
     <TypeName Value="12">666</TypeName> 
     <TypeName Value="13">777</TypeName> 
     <TypeName Value="14">888</TypeName> 
    </Type> 
    <Location> 
     <Area Value="0">Inside</Area> 
     <Area Value="1">OutSide</Area> 
     <Area Value="2">NoWhere</Area> 
    </Location> 
    </Product> 
</Test1> 

我想讓您可以在下拉菜單中選擇產品。然後根據產品,在另一個下拉列表中獲得一個類別。然後鍵入另一個,並在最後一個位置。我已經能夠讓產品在下拉菜單中顯示。但是無法根據產品獲得剩餘的元素。

我的代碼產品加載到一個下拉:

XmlDataset.ReadXml(Server.MapPath("XmlFile1.xml")); 

drpProducts.DataSource = XmlDataset.Tables["Product"]; 

drpProducts.DataTextField = "Name"; 
drpProducts.DataValueField = "Value"; 
drpProducts.DataBind(); 

我找不出下一個步驟。我該怎麼去做呢?

回答

1

這裏是解決方案,使用LINQ to XML。思路如下:

  1. 首先,將所有產品加載到Product Dropdown
  2. Product Dropdown更改時,獲取新值並將適當的數據填充到其餘下拉列表中。

這是代碼。我使用Windows Application創建演示程序。但這並不重要,只關注我如何選擇和填充數據。

首先聲明:

private XDocument _document; 

運行該功能時,首先負載:

private void LoadData() 
{ 
    // Load the document 
    _document = XDocument.Load("Product.xml"); 

    // Get the root's children, here is the products 
    IEnumerable<XElement> products = _document.Root.Elements(); 

    // Get product name 
    var productNames = new List<string>(); 
    foreach (var element in products) 
    { 
     productNames.Add(element.Attribute("Name").Value); 
    } 

    // Assign to the source 
    ddlProduct.DataSource = productNames; 
} 

當產品名稱更改,加載相應的類別。

private void ddlProduct_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    // New value 
    string productName = ddlProduct.Text; 

    // Ignore empty value, or your XPath would be wrong 
    if (String.IsNullOrEmpty(productName)) 
    { 
     return; 
    } 

    // Build the XPath 
    string xPath = string.Format("//Product[@Name='{0}']", productName); 

    // Select product with that name 
    XElement product = _document.XPathSelectElement(xPath); 

    // Select the CategoryType 
    IEnumerable<XElement> categories = product.Descendants("CatagoryType"); 

    // Get the name 
    var categoryNames = new List<string>(); 
    foreach (var category in categories) 
    { 
     categoryNames.Add(category.Value); 
    } 

    // Assign to source 
    ddlCategory.DataSource = categoryNames; 
} 

爲了使答案簡單,我只填充Category。其他人是一樣的。

如果您想了解更多關於XPath語法的知識,請看這link。有關LINQ to XML的更多信息,請檢查tutorial

+0

很好的答案。完美作品 – Sam

+0

謝謝!高興聽到 :) – AnhTriet

0

你能以這種方式嘗試: -

XmlDocument xdoc=new XmlDocument(); 
xdoc.Load("XmlFile1.xml"); 

//XmlNodeList node = xdoc.SelectSingleNodes("/Test1/Product/"); 

var node = document.SelectNodes("/Test1/Product"); 

foreach(XmlNode n in node) 
{ 
ListItem l = new ListItem(); 
    l.Text = n.InnerXml.ToString(); 
    drpWerk.Items.Add(l); 
} 
drpProducts.DataBind(); 
+0

再次出現錯誤「表達式必須評估爲節點集」。 – Sam

+0

你可以嘗試更改代碼 –

+0

相同的錯誤消息 – Sam

0

XML包含7個表時inported到DataSet enter image description here

在表中的列名在這裏顯示 enter image description here

的值位於TypeName表中。我使用VS中的Watch項目來查看DataSet對象。