簡體   English   中英

如何在C#中讀取xml並綁定到數據集

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM