0
我試圖添加一個新元素iconlink作爲子元素的每個佈局元素。我使用Xpath表達式來選擇節點,然後追加一個孩子。但每次它被追加到最後佈局只。 有人可以幫我找出問題所在。謝謝。未添加XML元素 - XPath
XmlNode newNode = layoutDocument.CreateElement("iconlink");
XmlAttribute imageAtr = layoutDocument.CreateAttribute("image");
imageAtr.Value = "CC_Status_Background.png";
newNode.Attributes.Append(imageAtr);
var nodeList = layoutDocument.SelectNodes("//Layout[@class]");
if (nodeList != null && nodeList.Count > 0)
{
foreach (XmlNode node in nodeList)
{
node.AppendChild(newNode);
layoutModified = true;
}
}
if (layoutModified)
{
//Save modified document
}
XML文件,如圖
<?xml version="1.0" encoding="utf-8"?>
<Layouts layoutid="161">
<Layout class="1">
<Page pagenumber="1" name="Page 1" />
<Page pagenumber="2" name="Page 2" />
<Page pagenumber="0" name="Page 0">
<iconlink id="iconlink_1262128931523" actionId="" tooltip="" image="CC_Favorite.png" required="true" />
<iconlink id="iconlink_1262128935340" actionId="" tooltip="" image="CC_Reject.png" required="true" />
<iconlink id="iconlink_1262128932676" actionId="" tooltip="" image="CC_Possible.png" required="true" />
<iconlink id="iconlink_1262128940983" actionId="" tooltip="" image="CC_Comment.png" required="true" />
<iconlink id="iconlink_1262128940984" actionId="" tooltip="" image="CC_Detail.png" required="true" />
<iconlink id="iconlink_1262128940982" actionId="" tooltip="" image="CC_Map.gif" required="true" />
</Page>
</Layout>
<Layout class="2">
<Page pagenumber="1" name="Page 1" />
<Page pagenumber="2" name="Page 2" />
<Page pagenumber="0" name="Page 0">
<iconlink id="iconlink_1262128931523" actionId="" tooltip="" image="CC_Favorite.png" required="true" />
<iconlink id="iconlink_1262128935340" actionId="" tooltip="" image="CC_Reject.png" required="true" />
<iconlink id="iconlink_1262128932676" actionId="" tooltip="" image="CC_Possible.png" required="true" />
<iconlink id="iconlink_1262128940983" actionId="" tooltip="" image="CC_Comment.png" required="true" />
<iconlink id="iconlink_1262128940984" actionId="" tooltip="" image="CC_Detail.png" required="true" />
<iconlink id="iconlink_1262128940982" actionId="" tooltip="" image="CC_Map.gif" required="true" />
</Page>
**<iconlink image="CC_Status_Background.png"></iconlink>**
</Layout>
</Layouts>
您是否試過了前。 '//佈局[@ class ='1']' –
你堅持使用Xpath嗎? LINQ to XML非常簡單(就是說,如果你對LINQ很熟悉) –
@FrancisDucharme LINQ to XML'也使用XPath(* xDocument.XPathSelectElement(....)*) –