我正在制作网络应用程序。我把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链接,它也包含了我的整个代码。