如何将我的xml文件排列到datatable然后绑定到gridview

时间:2013-01-17 15:46:25

标签: asp.net web-services

我正在制作网络应用程序。我把httpresponse放到xml文件中,所以我想首先将这个xml文件排列在datatable中然后绑定到网格视图中。

这是我的代码: -

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{

     StreamReader responsereader = new StreamReader(response.GetResponseStream());

     var responsedata = responsereader.ReadToEnd();
     xmldoc = (XmlDocument)JsonConvert.DeserializeXmlNode(responsedata);
     xmldoc.Save(@"C:\New folder\myfile.xml");

         DataTable dt = new DataTable();
         DataRow dr;
         dt.Columns.Add("hotelId");
         dt.Columns.Add("name");
         dt.Columns.Add("address1");
         dt.Columns.Add("address2");
         dt.Columns.Add("city");
         dt.Columns.Add("postalCode");
         dt.Columns.Add("countryCode");
         dr = dt.NewRow();

         XmlSerializer serializer = new XmlSerializer(typeof(HotelListResponse));
         HotelListResponse hx = new HotelListResponse();
         hx.Items = new hoteltype[7];

       Stream reader = new FileStream(@"C:\New folder\myfile.xml", FileMode.Open);

       HotelListResponse hlist = (HotelListResponse)serializer.Deserialize(reader);

           dt.ReadXml(@"C:\New folder\myfile.xml");
           //foreach(hoteltype ht in hlist.htype){
           this.GridView1.Visible = true;
           GridView1.DataSource = dt;
           GridView1.DataBind();
           //} 

          //responsereader.Close();
           //request.GetResponse().Close();

       }
   }
     catch (WebException ex)
     {
         if (ex.Response == null)
             throw new NullReferenceException("WebException response");
         throw ex;
     }

}

这里HotelListResponse我的类file.actually在程序中我使用自定义属性。我发布了pastbin链接,它也包含了我的整个代码。

http://pastebin.com/C2q4JYYt

0 个答案:

没有答案