简体   繁体   English

如何在C#中读取xml并绑定到数据集

[英]how to read xml and bind to dataset in c#

在此处输入图片说明 Actually i have some problem while accessing the xml Nodes i want to find Child Nodes from Root Node Following mine Code 实际上,我在访问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);
                }

This is Above code Actually my dt.Rows.Add(dtrow); 这是上面的代码,实际上是我的dt.Rows.Add(dtrow); Filled Same values in SalesRank ,ListPrice & AvailabilityType Attribute 在SalesRank,ListPrice和AvailabilityType属性中填充相同的值

So how can I resolve this Problem? 那么我该如何解决这个问题呢?

I think there is no need of for loop inside the foreach. 我认为在foreach中不需要for循环。 if you eliminate the for loop and write the statement you write inside the for loop directly in foreach loop by modifying it as follows then you will get the correct result. 如果消除了for循环并通过如下修改直接在foreach循环中编写在for循环中编写的语句,那么您将获得正确的结果。

dtrow["colname"]=salesnode.InnerXml dtrow [“ colname”] = salesnode.InnerXml

otherwise remove the foreach loop and for loop will do what exactly you want. 否则,删除foreach循环,for循环将完全按照您的要求进行。

Following my is code that has solved my problem: 以下是解决我的问题的代码:

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM