开发者

Replace Element with XDocument Instead of XmlDocument

开发者 https://www.devze.com 2023-03-25 08:15 出处:网络
Using XDocument, how could the following code be rewritten? This code replaces the element values with strValues. The element is specified by strKeys and strXPath = \"/root/node/subnode[param1=\'value

Using XDocument, how could the following code be rewritten? This code replaces the element values with strValues. The element is specified by strKeys and strXPath = "/root/node/subnode[param1='value1' and param2='value2']".

    public static void ReplaceXmlElement(string strXPath, stri开发者_StackOverflow中文版ng[] strKeys, string[] strValues)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(xmlFile.xml);
        XmlNode xNode = xDoc.SelectSingleNode(strXPath);
        int intTemp = 0;
        foreach (XmlNode node in xNode)
        {
            node.Name.ToString();
            if (node.Name == strKeys[intTemp])
            {
                node.InnerXml = strValues[intTemp];
                intTemp++;
            }
        }
        xDoc.Save(xmlFile.xml);
    }


http://msdn.microsoft.com/en-us/library/bb156083.aspx

XElement root = new XElement("Root",
new XElement("Child1", 1),
new XElement("Child2", 2),
new XElement("Child3", 3),
new XElement("Child4", 4),
new XElement("Child5", 5),
new XElement("Child6", 6)
);

XElement el = root.XPathSelectElement("./Child4");
0

精彩评论

暂无评论...
验证码 换一张
取 消