簡體   English   中英

用XDocument而不是XmlDocument替換Element

[英]Replace Element with XDocument Instead of XmlDocument

使用XDocument ,如何重寫以下代碼? 此代碼用strValues替換元素值。 該元素由strKeysstrXPath = "/root/node/subnode[param1='value1' and param2='value2']"

    public static void ReplaceXmlElement(string strXPath, string[] 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/zh-CN/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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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