0

我在名为 getval 的字符串中有响应 xml。
我想选择所有“fromsector”节点值并将该值替换为数据库中的相应值。例如:Ban应该替换为bangalore..

我尝试了 XmlNodeList 和 XmlNode。但他们都没有工作。请帮忙。

C#

 XmlDocument doc = new XmlDocument();
                    doc.Load(Server.MapPath("~/XMLFile1.xml"));
                    XmlNodeList fromselectors;
                    XmlElement root = doc.DocumentElement;
                    fromselectors = root.SelectNodes("SearchResults/FlightSegments/Flights/LegDetails/Leg/FromSector");
                    foreach (XmlNode n in fromselectors)
                    {
                        con.Open();
                        cmd.Connection = con;
                        string xmls = n.InnerXml;
                        cmd.CommandText = "select City from CCode where Code='" + xmls + "'";
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    doc.Save(Server.MapPath("~/XMLFile1.xml"));
4

1 回答 1

2

使用 XPath 从 xml 中获取所需的节点并更新值

        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/temp.xml"));

        //Selecting node with number='3'
        XmlNodeList fromselectors;
        XmlElement root = doc.DocumentElement;
        fromselectors = root.SelectNodes("SearchResults/FlightSegments/Flights/LegDetails/Leg/FromSector");

        con.Open();
        cmd.Connection = con;
        foreach (XmlNode n in fromselectors)
        {                                   
           string xmls = n.InnerXml;
           cmd.CommandText = "select City from CCode where Code='" + xmls + "'";
           string city = cmd.ExecuteScalar().ToString();
           n.InnerText = city;
        }
        con.Close();
        doc.Save(Server.MapPath("~/temp.xml"));
于 2013-10-08T04:57:17.490 回答