问题描述:

My Xml file looks like this.

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

I am trying to make which you can select a Product in a drop down. Then based on the product, get a category in another dropdown. Then Type in another, and location in the last. I have been able to get the Products to show in a drop down. But not able to get the rest of the elements in based on the product.

My code to load products into a drop down:

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

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

drpProducts.DataTextField = "Name";

drpProducts.DataValueField = "Value";

drpProducts.DataBind();

I cannot figure the next steps. How would I go about doing it?

网友答案:

Here is the solution, using LINQ to XML. The idea presented in the following steps:

  1. First, load all product to Product Dropdown.
  2. When the Product Dropdown is changed, get the new value and populate appropriate data to the rest dropdown.

Here is the code. I create a demo program using Windows Application. But it's not important, just focus on how I select and populate data.

First, declaration:

private XDocument _document;

Run this function when first load:

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

When product name change, load the appropriate category.

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

To make the answer simple, I only populate Category. The others are the same.

If you want to know more about XPath syntax, look at this link. And for more information about LINQ to XML, check this tutorial.

网友答案:

Can you try in this way:-

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();
网友答案:

XML contains 7 tables when inported to a DataSet

The column names in the table are shown here

The Value is in the TypeName Table. I use the Watch item in VS to view the DataSet object.

相关阅读:
Top