DVM
DVM

Reputation: 1238

Split xml file based on nodes

I have an XML doc which looks roughly like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Multiple xmlns:ns2="someNs2" xmlns="someGenericNs" xmlns:ns4="someNs4" xmlns:ns3="someNs3">
  <Single>
    <Id>60000</Id>
    <Type>Activate</Type>
    <Payload>
      <ns3:Activation>
        <ns3:Parent>
          <ns3:TypeId>113</ns3:TypeId>
          <ns3:TypeName>TestApplication</ns3:TypeName>
        </ns3:Parent>
        <ns3:Children>
          <ns3:Child>
            <ns3:Key>someKey</ns3:Key>
            <ns3:ChildTypeName>BadAppType1</ns3:ChildTypeName>
          </ns3:Child>
          <ns3:Child>
            <ns3:Key>someOtherKey</ns3:Key>
            <ns3:ChildTypeName>GoodAppType1</ns3:ChildTypeName>
          </ns3:Child>
        </ns3:Children>
      </ns3:Activation>
    </Payload>
  </Single>
</Multiple>

If the file contains multiple "Child" nodes i would like to split it into more files, 1 file for each existing child node. Something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Multiple xmlns:ns2="someNs2" xmlns="someGenericNs" xmlns:ns4="someNs4" xmlns:ns3="someNs3">
  <Single>
    <Id>60000</Id>
    <Type>Activate</Type>
    <Payload>
      <ns3:Activation>
        <ns3:Parent>
          <ns3:TypeId>113</ns3:TypeId>
          <ns3:TypeName>TestApplication</ns3:TypeName>
        </ns3:Parent>
        <ns3:Children>
          <ns3:Child>
            <ns3:Key>someOtherKey</ns3:Key>
            <ns3:ChildTypeName>GoodAppType2</ns3:ChildTypeName>
          </ns3:Child>
        </ns3:Children>
      </ns3:Activation>
    </Payload>
  </Single>
</Multiple>

And then a new XmlDoc containing the other "Child" node. Can this be achieved through LINQ?

My code so far is:

 private bool HasMoreThanOneChild(string xml) {
          XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

                if (doc.GetElementsByTagName("ns3:Child").Count > 1)
                {
                    return true;
                }

        return false;
    }

public List<string> DoSomething(XmlDocument doc){

if(HasMoreThanOneChild(doc.InnerXml))
return Split(doc);
}

I returned a List there because I'm more interested in the "InnerXML" once the doc is split. But I am at a loss of how to implement this split, if it is possible.

Upvotes: 0

Views: 2338

Answers (1)

Anirudha
Anirudha

Reputation: 32817

XNamespace ns="someNs3";

//get all Child elements
var childElements=doc.Descendants().Elements(ns+"Child").ToList();

//remove those child elements
doc.Descendants().Elements(ns+"Child").ToList().ForEach(x=>x.Remove());

int i=1;
foreach(var child in childElements)
{
    //add that child to children element
    doc.Descendants().Elements(ns+"Children").First().Add(child);
    //save it to new file!
    doc.Save("file"+i+".xml");
    doc.Descendants().Elements(ns+"Child").ToList().ForEach(x=>x.Remove());
    i++;
}

Upvotes: 1

Related Questions