Reputation: 97
I am trying to add a new element iconlink as a child of each Layout element. I am using Xpath expression to select nodes and then Append a child. But every time it is appended to last Layout only. Can someone help me find out what the issue is. Thanks.
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 File is as shown
<?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>
Upvotes: 0
Views: 87
Reputation: 25855
You are thinking of the node as text, but in the DOM the node is a specific instance of an element. You have created a single instance of the element and then you are trying to add it to multiple parents. An XML element can have only one parent, so it looks like your code is just moving it around. Try creating a new instance for each parent:
var nodeList = layoutDocument.SelectNodes("//Layout[@class]");
if (nodeList != null && nodeList.Count > 0)
{
foreach (XmlNode node in nodeList)
{
XmlNode newNode = layoutDocument.CreateElement("iconlink");
XmlAttribute imageAtr = layoutDocument.CreateAttribute("image");
imageAtr.Value = "CC_Status_Background.png";
newNode.Attributes.Append(imageAtr);
node.AppendChild(newNode);
layoutModified = true;
}
}
Upvotes: 2