简体   繁体   中英

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

  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); Filled Same values in SalesRank ,ListPrice & AvailabilityType Attribute

So how can I resolve this Problem?

I think there is no need of for loop inside the foreach. 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.

dtrow["colname"]=salesnode.InnerXml

otherwise remove the foreach loop and for loop will do what exactly you want.

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();
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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