1

I have a xml file. I trying generate a new xml file from that old xml file with some modification.

I have xml like this

<Parent>
<child>
  <child 1 />
  <child 2 />
  <child 3>
      <child A1 />
      <child A2 />
      <child A3 />
  </child 3 >
  <child 4 />
  <child 5 />
</child>
<Parent>

I trying to generate a new xml file like this

<Parent>
 <Child>
  <child 1/>
  <child 3/>
  <child 3/>
      <child A1 />
      <child A2 />
      <child A3 />
  </child 3 >
  <child 4 />
  <child 5 />
 </Child>
 <Child>
  <child 1 />
  <child 2 />
  <child 3>
      <child A1 />
      <child A2 />
      <child A3 />
  </child 3 >
  <child 4 />
  <child 5 />
 </Child>
</Parent>

I tried below code

XmlReader xmlreader;
if (xmlTextReader.ReadToFollowing("Parent"))  
{                   
 XmlReader inner = xmlTextReader.ReadSubtree();
 while (isStartElement)
 {
 inner.Read();
 switch (xmlTextReader.NodeType)
 {
 case XmlNodeType.Element:
     xmlreader = inner.ReadSubtree();                           
     xmlreader.ReadToDescendant("Parent");
     xmlreader.Read();

     do
     {
       if (xmlTextReader.Name != "Parent")
       {
          xmlTextWriter.WriteStartElement(xmlTextReader.Prefix, xmlTextReader.LocalName,xmlTextReader.NamespaceURI);
       }
       if (xmlTextReader.HasAttributes)
       {
            while (xmlTextReader.MoveToNextAttribute())
            {
              xmlTextWriter.WriteAttributeString(xmlTextReader.Name,xmlTextReader.Value.ToString());
            }
        }

     xmlTextWriter.WriteEndElement();

   } while (inner.ReadToNextSibling("Child"));                              

    break;
  }
 }
}

I tried above code but it get only return below xml

<Child />
<Child />

It return on parent node of child1 child2 etc. nodes

I am using 2 xmlreader for read the xml

I need below xml output like

<Child>
  <child 1/>
  <child 3/>
  <child 3/>
      <child A1 />
      <child A2 />
      <child A3 />
  </child 3 >
  <child 4 />
  <child 5 />
 </Child>
 <Child>
  <child 1 />
  <child 2 />
  <child 3>
      <child A1 />
      <child A2 />
      <child A3 />
  </child 3 >
  <child 4 />
  <child 5 />
 </Child>

I need some code in do while for getting child nodes and then close the parent node. so i will get desire output

  • You need to read the xml recursively. The following example puts the xml data into a tr3eview. You need to to similar except instead of loading a treeview print results : https://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Feb 17 '20 at 13:48
  • Thank you for sharing but i need it from xmlreader. i cannot accept this solution with recursive because i have a large XML file something around 1 GB – Milind Varne Feb 17 '20 at 14:06
  • I don't understand what you're trying to accomplish here. Are you trying to **duplicate** the entire `` hierarchy within ``? If so you'll need to load `` into memory and then write it twice, since `XmlReader` doesn't support rewinding. Or if that's not what you are trying to do, can you explain in more detail? – dbc Feb 17 '20 at 23:11
  • Yes, I am trying to duplicate entire child node in tag using xmlreader – Milind Varne Feb 18 '20 at 04:00
  • @MilindVarne - can you load the entire `` node into memory at once, or is it so huge that you cannot? – dbc Feb 18 '20 at 18:42

0 Answers0