You must Sign In to post a response.
  • Category: XML

    How to Add Additional parent in existing XML document


    Are you looking for a way to Add Additional parent in existing XML document ? then read this thread to know how to do it



    Hi All,
    Here I have document like below.Here the xml document having a tag <carrier:Header> which is having two child.Now i want to add one more child to <carrier:Header> but at the same time it will be sub parent for existing child elements.For example,it should like below.how to make it.please help me.

    <carrier:Header>
    <carrier:Headercompany1>
    <infra:MessageId>enroll</infra:MessageId>
    <infra:Timestamp>2015-02-23</infra:Timestamp>
    <carrier:Headercompany1>
    </carrier:Header>


    <EnrollmentRequest xsi:schemaLocation="http://iws.fuwt.org/enrollment/service" xmlns="http://iws.fuwt.org/enrollment/service" xmlns:core="http://iws.fuwt.org/enrollment/Core" xmlns:carrier="http://iws.fuwt.org/enrollment/Core/Carrier" xmlns:group="http://iws.fuwt.org/enrollment/Core/Group" xmlns:infra="http://iws.fuwt.org/enrollment/Core/Infra" xmlns:simple="http://iws.fuwt.org/enrollment/Core/SimpleTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <carrier:Header>
    <infra:MessageId>enrollmentUpsert</infra:MessageId>
    <infra:Timestamp>2015-02-23T12:00:58-05:00</infra:Timestamp>
    </carrier:Header>
    <carrier:Enrollment>
    <carrier:TenantID>1</carrier:TenantID>
    <carrier:GroupNumber>123</carrier:GroupNumber>
    <carrier:FirstName>amal</carrier:FirstName>
    <carrier:OfficePhone/>
    <carrier:TribalID/>
    <carrier:SubscriberAddresses>
    <core:Address>
    <core:AddressType>M</core:AddressType>
    <core:EffectiveDate>2015-03-01</core:EffectiveDate>
    <core:AddressLine1>29 Winthrop Road</core:AddressLine1>
    <core:AddressLine2/>
    <core:AddressLine3/>
    <core:City>Short Hills</core:City>
    <core:State>NJ</core:State>
    <core:ZipCode>07078</core:ZipCode>
    </core:Address>
    </carrier:SubscriberAddresses>
    <carrier:Groupc>44545431</carrier:Groupc>
    </EnrollmentRequest>
  • #757338
    You need to create a second xml and then add your first xml as child of second xml, create a two separate xml and then create their XMLDocument
    Now, you need to use 'DocumentElement.AppendChild' to append child in documentA


    XmlDocument documentA;
    XmlDocument documentB;

    foreach(var childNode in documentA.DocumentElement.ChildNodes)
    documentB.DocumentElement.AppendChild(childNode);

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763000
    This may give you a rough idea. Hope this helps.

    Nodes childNodes = parent.query("child");
    for (int i = 0; i < childNodes.size(); i++) {
    Element currentChild = (Element) childNodes.get(i);
    if (someCondition) {
    ParentNode parent = currentChild.getParent();
    int currentIndex = parent.indexOf(currentChild);
    currentChild.detach();
    Element extraParent = new Element("extraParent");
    extraParent.appendChild(currentChild);
    parent.insertChild(extraParent,currentIndex);
    }
    }


  • Sign In to post your comments