Learner
Learner

Reputation: 571

How to put nodes into parent node?

I would like to add a parent node with attribute and put nodes inside of this parent node. I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <row>
      <PHONE_NUM>124</PHONE_NUM>
      <PHONE_ORDER>1</PHONE_ORDER>      
   </row>
   <row>
      <PHONE_NUM>123</PHONE_NUM>
      <PHONE_ORDER>2</PHONE_ORDER>      
   </row>
   <row>
      <PHONE_NUM>181</PHONE_NUM>
      <PHONE_ORDER>3</PHONE_ORDER>      
   </row>   
</foo>

I've tried this approach:

var xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <foo>
       <row>
          <PHONE_NUM>124</PHONE_NUM>
          <PHONE_ORDER>1</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>123</PHONE_NUM>
          <PHONE_ORDER>2</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>181</PHONE_NUM>
          <PHONE_ORDER>3</PHONE_ORDER>      
       </row>   
    </foo>";

var report = XDocument.Parse(xmlString);
var newdoc = new XDocument();
newdoc.Add(new XElement("testsuites")); 
newdoc.Root.Add(report.Root);

But the above code generates is withoud id and order books has become a root :

<books>
  <foo>
      <row>
          <PHONE_NUM>124</PHONE_NUM> 
          <PHONE_ORDER>1</PHONE_ORDER> 
      </row>
      <row>
          <PHONE_NUM>123</PHONE_NUM> 
          <PHONE_ORDER>2</PHONE_ORDER> 
      </row>
      <row>
          <PHONE_NUM>181</PHONE_NUM> 
          <PHONE_ORDER>3</PHONE_ORDER> 
      </row>
      </foo>
  </books>

What I am trying to achieve is:

<?xml version=""1.0"" encoding=""UTF-8""?>
<foo>
    <books id=""1"">
       <row>
          <PHONE_NUM>124</PHONE_NUM>
          <PHONE_ORDER>1</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>123</PHONE_NUM>
          <PHONE_ORDER>2</PHONE_ORDER>      
       </row>
       <row>
          <PHONE_NUM>181</PHONE_NUM>
          <PHONE_ORDER>3</PHONE_ORDER>      
       </row>  
    </books>
</foo>

If it is possible, it would be great if XmlDocument class will be used. Any help would be greatly appreciated.

Upvotes: 1

Views: 53

Answers (2)

Jawad
Jawad

Reputation: 11364

This process helps you create a new child and move all nodes under this new child.

  1. create a new element first..
  2. Move all the nodes under this new element
  3. Add the new element where you needed in the XML.
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(xmlString);

var newElement = doc2.CreateElement("body");
newElement.SetAttribute("id", "1");

var moveNodes = doc2.DocumentElement.SelectNodes("//row");
foreach(XmlNode node in moveNodes)
    newElement.AppendChild(node);

doc2.GetElementsByTagName("foo")[0].AppendChild(newElement);

Output

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <body id="1">
        <row>
            <PHONE_NUM>124</PHONE_NUM>
            <PHONE_ORDER>1</PHONE_ORDER>
        </row>
        <row>
            <PHONE_NUM>123</PHONE_NUM>
            <PHONE_ORDER>2</PHONE_ORDER>
        </row>
        <row>
            <PHONE_NUM>181</PHONE_NUM>
            <PHONE_ORDER>3</PHONE_ORDER>
        </row>
    </body>
</foo>

Upvotes: 2

Jesse de Wit
Jesse de Wit

Reputation: 4177

Here's about what you want to do, using the XDocument class. It's simply a matter of creating new nodes and modifying existing nodes with your specification:

var report = XDocument.Parse(xmlString);
report.Root.Name = XName.Get("books", report.Root.Name.NamespaceName);
report.Root.Add(new XAttribute("id", 1));
var newDoc = new XDocument();
var newRoot = new XElement("foo");
newRoot.Add(report.Root);
newDoc.Add(newRoot);

Upvotes: 1

Related Questions