通过Xpath和Bind在嵌套的GridView中搜索Xml NodeList

时间:2015-05-26 22:20:16

标签: c# asp.net xml xpath xmldocument

我只想将xml绑定到gridview中,这个多个子节点就像这样..

<?xml version="1.0" encoding="utf-8" ?>
<Products>
<Product>
    <SKU>DL-DL4002</SKU>
    <Name>Leopard Print Strapless Corset Top with G-string</Name>
    <ImagePath4>
      http://da.com/images/product/large/DL-DL4002_4_.jpg
    </ImagePath4>
    <Variants>
      <Variant>
        <Size>S</Size>
        <Cost>25.28</Cost>
        <MSRP>57.45</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
      <Variant>
        <Size>M</Size>
        <Cost>25.28</Cost>
        <MSRP>57.45</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
      <Variant>
        <Size>L</Size>
        <Cost>25.28</Cost>
        <MSRP>57.45</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
      <Variant>
        <Size>XL</Size>
        <Cost>25.28</Cost>
        <MSRP>57.45</MSRP>
        <Avialability>out of stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
    </Variants>
  </Product>
  <Product>
    <SKU>DL-DL4003</SKU>
    <Name>Designer Purple Brocade Party Corset Top</Name>

    <ImagePath4>
      http://da.com/images/product/large/DL-DL4003_4_.jpg
    </ImagePath4>
    <Variants>
      <Variant>
        <Size>S</Size>
        <Cost>23.1</Cost>
        <MSRP>52.5</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
      <Variant>
        <Size>M</Size>
        <Cost>23.1</Cost>
        <MSRP>52.5</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
      <Variant>
        <Size>L</Size>
        <Cost>23.1</Cost>
        <MSRP>52.5</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
      <Variant>
        <Size>XL</Size>
        <Cost>23.1</Cost>
        <MSRP>52.5</MSRP>
        <Avialability>in stock</Avialability>
        <Weight>0.6</Weight>

      </Variant>
    </Variants>
  </Product>
</Products>

,当我用这样的gridview绑定它时,它没问题

 XmlDocument xmldoc = new XmlDocument();
  xmldoc.Load(Server.MapPath("~/XML/XM.xml"));
  XmlNodeList nodeList = xmldoc.SelectNodes("/Products/Product");

     foreach (XmlNode node in nodeList)
                {
                     DataRow dtrow = dt.NewRow();
                    dtrow["SKU"] = node["SKU"].InnerText;
                }

没问题,但是当我在gridview中为嵌套gridview绑定Row DataBound上的Varients

时 像那样,

protected void GridXML_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            GridView gv = (GridView)e.Row.FindControl("gvChildGrid");
            DataTable dt = new DataTable();
        XmlNodeList nodeList = xmldoc.DocumentElement.SelectNodes("/Products/Product/Variants/Variant");
        }
}

它将为两个对象选择所有变体节点,即产品SkU ID = DL-DL4000和DL-DL4002,但我只想选择一个对象,即逐个

请帮忙

1 个答案:

答案 0 :(得分:0)

基本上,您可以使用xpath谓词( h4 { color: blue; } article{ color: black; } aside{ color: purple; } aside p{ color: gray; } aside h4{ font-style: italic !important; color: yellow; } article h4{ color: brown; } section h4{ color: orange; } )来过滤特定条件的元素。例如:

[]

上面的示例将从string sku = "DL-DL4002"; string xpath = String.Format("/Products/Product[SKU='{0}']/Variants/Variant", sku); XmlNodeList nodeList = xmldoc.DocumentElement .SelectNodes(xpath); Variant返回ProductSKU等于"DL-DL4002"。我不熟悉ASP.NET gridview,但您应该能够从SKU参数中获取当前e.Row值。