如何在网格视图中显示(WS响应)的xmlnode值?

时间:2012-09-08 06:24:00

标签: c# xml web-services rest amazon-web-services

我在我的网络应用程序中使用AWS。我得到了WS响应。

Xml回复:

 <ItemSearchRespone xmlns="http:/....">
  <Items>
   <Item>
  <ASIN>B003MC5N28</ASIN>
  <DetailPageURL>http://www.amazon.com/Giver-Newbery-Medal-Book-ebook/dp/B003MC5N28%3FSubscriptionId%3DAKIAIETT7RP2RAFF4UUQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB003MC5N28</DetailPageURL>
  <SalesRank>469</SalesRank>
   **<ItemAttributes>**
      <Address>
      <Country>us</Country>
      </Address>
    <Author>Lois Lowry</Author>
    <Binding>Kindle Edition</Binding>
    <Country>us</Country>
    <Format>Kindle eBook</Format>
    <Label>Houghton Mifflin Books for Children</Label>
     <ListPrice>
        <Amount>895</Amount>
      <CurrencyCode>USD</CurrencyCode>
      <FormattedPrice>$8.95</FormattedPrice>
     </ListPrice>
      <Manufacturer>Houghton Mifflin Books for Children</Manufacturer>
    <NumberOfItems>1</NumberOfItems>
    <NumberOfPages>204</NumberOfPages>
    <ProductGroup>eBooks</ProductGroup>
    <PublicationDate>1993-04-26</PublicationDate>
    <Publisher>Houghton Mifflin Books for Children</Publisher>
    <ReadingLevel>Young Adult</ReadingLevel>
    <ReleaseDate>1993-04-26</ReleaseDate>
    <Studio>Houghton Mifflin Books for Children</Studio>
    <Title>The Giver (Newbery Medal Book)</Title>
  **</ItemAttributes>**
  </Item>
  <Item>  another item list2</Item>
  <Item>  another item list<3.../Item>
 </Items>
  </ItemSearchResponse>

在这里,我想在网格视图中显示所有书籍的所有标题值和格式化价格。

到目前为止我已经尝试过了。

        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(response.GetResponseStream());
        XmlNamespaceManager ns = new XmlNamespaceManager(xdoc.NameTable);
        ns.AddNamespace("ms", "http://webservices.amazon.com/AWSECommerceService/2005-10-05");

        XmlNodeList xnl = xdoc.SelectNodes("//ms:Title", ns);
        XmlNodeList xnl1 = xdoc.SelectNodes("//ms:FormattedPrice", ns);



        DataTable dt=new DataTable();
        dt.Columns.Add("Title",typeof(string));
        dt.Columns.Add("Price", typeof(string));

        foreach (XmlNode Title in xnl)
        {
            DataRow dr = dt.NewRow();
            dr["Title"] = Title.InnerText;

            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();

如何在网格视图中显示价格和标题值? (Respone有很多节点,如title,Price,isbn等等。如果您需要更多信息,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:1)

LINQ to XML解析XML文档。

XNamespace ns = "http://webservices.amazon.com/AWSECommerceService/2005-10-05";
XDocument doc=XDocument.Load(response.GetResponseStream());

var result= doc.Descendants(ns + "ItemAttributes").Select( p=> new 
 {
 Title=p.Element(ns+ "Title").Value,
 Price = p.Element(ns +"ListPrice").Element(ns+"FormattedPrice").Value
}).ToList();
GridView1.DataSource = result;
GridView1.DataBind();

或更改XPath表达式,

 XmlNodeList xnl = xdoc.SelectNodes("/ms:ItemSearchRespone/ms:Items/ms:Item/ms:ItemAttributes", ns);

  foreach (XmlNode ele in xnl)
   {
     Console.WriteLine(ele.SelectSingleNode("ms:Title", ns).InnerText);
     Console.WriteLine(ele.SelectSingleNode("ms:ListPrice/ms:FormattedPrice", ns).InnerText);
   }