i wish you a happy new year! I got following XML structure:
<?xml version="1.0" encoding="UTF-8"?>
<SW.CodeBlock ID="0">
<SW.CompileUnit ID="1">
<AttributeList>
<NetworkSource>
<FlgNet xmlns="http://www.TEST.com">
<Parts> </Parts>
</FlgNet>
</NetworkSource>
</AttributeList>
</SW.CompileUnit>
<SW.CompileUnit ID="2">
<AttributeList>
<NetworkSource>
<FlgNet xmlns="http://www.TEST.COM">
<Parts> </Parts>
</FlgNet>
</NetworkSource>
</AttributeList>
</SW.CompileUnit>
</SW.CodeBlock>
How can i add a Child in "Parts" from SW.CompileUnit ID = 1 and SW.CompileUnit ID = 2 etc.?
I would like to create a loop (for-loop), which creates a child in "Parts" for every "SW.CompileUnit"-Node
Could you please help me?
PS: I use VS2015, C#, not using Linq or XPath etc.
Until now I add a child like this:
XmlNode xPiece = xdoc.SelectSingleNode("//NS2:Parts",nsmgr);
xPiece.AppendChild(myXMLElement);
but it only adds a child in the first SW.CompileUnit Node (with the ID=1) ...
Thanks in advance
SelectSingleNode()
returns only the first matched elements. To get all matched elements, you're supposed to use SelectNodes()
instead :
var nodes = xdoc.SelectNodes("//NS2:Parts",nsmgr);
foreach(XmlNode node in nodes)
{
//create new myXMLElement
....
//and then append it to current <Parts>
node.AppendChild(myXMLElement);
}
Btw, parameter of SelectNodes()
and SelectSingleNode()
are XPath expressions (just saying, because you wrote "I use VS2015, C#, not using Linq or XPath etc").