Solved

Add Node to XmlDocument

Posted on 2004-11-01
503 Views
Last Modified: 2008-01-16
Hi

I have the following structure
<Root>
<Person value='10'>
<DetailInfo value='abcd'>

</DetailInfo>
<Addresses>
</Addresses>
</Person>
<Person value='20'>
<DetailInfo value='abcd'>

</DetailInfo>
<Addresses>
</Addresses>
</Person>
<Person value='30'>
<DetailInfo value='abcd'>

</DetailInfo>
<Addresses>
</Addresses>
</Person>
</Root>

I want to add the  Rate and Grade to DetailInfo node based on person value and detail info value and
need to add address node to addresses node based on person value.
<Address>
<State></State>
<City></City>
<Country></Country>
</Address>

If anyone have clue pls help me.
Thanks
Ravi
0
Question by:pprasadravi
    4 Comments
     
    LVL 7

    Expert Comment

    by:J_Mak
    Are you using .NET to do this? Please explain your situation. Cheers.
    0
     
    LVL 6

    Expert Comment

    by:sathishv
    If you are using JavaScript,

    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.load("test.xml");
    personValue = 20;
    detailValue = 'abcd';
    var detailNode = xmlDoc.selectSingleNode("//DetailInfo[./Person[@value='" + personValue + "'] and ./DetailInfo[@value='" + detailValue + "']]");
    var addressNode = detailNode.nextSibling;
    var newNode = xmlDoc.createElement("Address");
    var newNode2 = xmlDoc.createElement("State"); newNode2.text = "";
    var newNode3 = xmlDoc.createElement("City"); newNode3.text = "";
    var newNode4 = xmlDoc.createElement("Country"); newNode4.text = "";
    newNode.appendChild(newNode2);
    newNode.appendChild(newNode3);
    newNode.appendChild(newNode4);
    addressNode.appendChild(newNode);

    The above strip of code selects a DetailInfo tag based on the given person value and detailinfo value, then selects the next sibling of the tag (which is the addresses tag) and appends the newly created elements to it.

    Hope this helps.

    Cheers,
    Sathish
    0
     

    Author Comment

    by:pprasadravi
    i am sorry we are using asp.net and c#.
    0
     
    LVL 7

    Accepted Solution

    by:
    Have a look at this link:

    http://samples.gotdotnet.com/quickstart/howto/doc/Xml/WriteXMLFile.aspx

    It should help you. Cheers.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now