1

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

2 Answers 2

1

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").

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your help, and yeah i think i just missed that part with Xpath ;)
0

Use XML Linq. Not sure if I got your request exactly correct.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<?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>";

            XDocument doc = XDocument.Parse(xml);
            var compileUnits = doc.Descendants("SW.CompileUnit").Select(x => new {
                ID = (string)x.Attribute("ID"),
                parts = x.Descendants().Where(y => y.Name.LocalName == "Parts").FirstOrDefault()
            }).ToList();
            foreach (var compileUnit in compileUnits)
            {
                compileUnit.parts.Add(new XElement(compileUnit.parts.Name.Namespace + "ID", compileUnit.ID));
            }
        }
    }
}

2 Comments

Thank you for your help! @har07's solution is fitting better, thank you though
I wan't sure if you wanted to take the id from the CompileUnit and insert into Parts. Where is the new ID number coming from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.