[英]how to read xml and bind to dataset in c#
实际上,我在访问xml节点时遇到一些问题,我想根据我的代码从根节点中查找子节点
string myXmlString = string.Empty;
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
XmlDocument doc = new XmlDocument();
doc.Load(response.GetResponseStream());
string Xml = doc.DocumentElement.InnerXml;
XmlNodeList xnList = doc.GetElementsByTagName("Item");
DataTable dt = new DataTable();
dt.Columns.Add("ASIN", typeof(string));
dt.Columns.Add("SalesRank", typeof(string));
dt.Columns.Add("ListPrice", typeof(string));
dt.Columns.Add("AvailabilityType", typeof(string));
foreach (XmlNode node in xnList)
{
DataRow dtrow = dt.NewRow();
dtrow["ASIN"] = node["ASIN"].InnerText;
XmlNodeList elemList = doc.GetElementsByTagName("SalesRank");
foreach (XmlNode salesNode in elemList)
{
for (int i = 0; i < elemList.Count; i++)
{
dtrow["SalesRank"] = elemList[i].InnerXml;
}
}
XmlNodeList ListPrice = doc.GetElementsByTagName("ListPrice");
foreach (XmlNode salesNode in ListPrice)
{
for (int i = 0; i < ListPrice.Count; i++)
{
dtrow["ListPrice"] = ListPrice[i].InnerText;
}
}
XmlNodeList AvailbleAttr = doc.GetElementsByTagName("AvailabilityType");
foreach (XmlNode AvlNode in AvailbleAttr)
{
if (AvlNode.Name == "AvailabilityType")
{
for (int i = 0; i < AvailbleAttr.Count; i++)
{
dtrow["AvailabilityType"] = AvailbleAttr[i].InnerText;
}
}
}
dt.Rows.Add(dtrow);
}
这是上面的代码,实际上是我的dt.Rows.Add(dtrow); 在SalesRank,ListPrice和AvailabilityType属性中填充相同的值
那么我该如何解决这个问题呢?
我认为在foreach中不需要for循环。 如果消除了for循环并通过如下修改直接在foreach循环中编写在for循环中编写的语句,那么您将获得正确的结果。
dtrow [“ colname”] = salesnode.InnerXml
否则,删除foreach循环,for循环将完全按照您的要求进行。
以下是解决我的问题的代码:
private void GetProductDetails()
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\ANDY\\Desktop\\XML Data\\Demo.xml");
XmlNode node = doc.DocumentElement.SelectSingleNode("/users");
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("ADD1", typeof(string));
//dt.Columns.Add("DEPT", typeof(string));
foreach (XmlNode NodeXml in node)
{
DataRow dtrow = dt.NewRow();
//dtrow["Name"] = NodeXml["Name"].InnerText;
XmlElement companyElement = (XmlElement)NodeXml;
dtrow["Name"] = companyElement.GetElementsByTagName("Name")[0].InnerText;
dtrow["ID"] = companyElement.GetElementsByTagName("ID")[0].InnerText;
dtrow["FirstName"] = companyElement.GetElementsByTagName("FirstName")[0].InnerText;
dtrow["LastName"] = companyElement.GetElementsByTagName("LastName")[0].InnerText;
dtrow["ADD1"] = companyElement.GetElementsByTagName("ADD1")[0].InnerText;
//xmlCompanyID = companyElement.Attributes["ID"].InnerText;
//this is ANother Method you can bind ADDR.USING Child Method
//XmlNode AddrNode = node.SelectSingleNode("/users/DEPT/LOCATION");
//for (int i = 0; i < AddrNode.ChildNodes.Count; i++)
//{
// if (AddrNode.ChildNodes[i].Name == "ADD1")
// {
// dtrow["ADD1"] = AddrNode["ADD1"].InnerText;
// }
//}
dt.Rows.Add(dtrow);
}
if (dt.Rows.Count > 0)
{
RepDetails.DataSource = dt;
RepDetails.DataBind();
}
else
{
RepDetails.DataSource = "";
RepDetails.DataBind();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.