Skip to main content

Microsoft Silverlight

Answered Question How to use XDocument to add, update, delete the xml node information?RSS Feed

(0)

spiderman110
spiderma...

Member

Member

225 points

282 Posts

How to use XDocument to add, update, delete the xml node information?

 Hi, all

I have a xml string like below:

<Product>
    <A id="001"/>
    <B id="002"/>
    <C id="003"/>
    <D id="004">
        <D1 id="0041" />
        <D2 id="0042" />
    </D>
    <E id="005"/>
</Product>


ex, I could add D3 node in  D node or delete D1 node from D node or modify the id "0042" to "0044" of D2 node

In a word, I want to handle somthing on D node.

If D3 node is added, I want to return a string value like this:

<Product>
    <A id="001"/>
    <B id="002"/>
    <C id="003"/>
    <D id="004">
        <D1 id="0041" />
        <D2 id="0042" />

        <D3 id="0043" />
    </D>
    <E id="005"/>
</Product>

 

I don:t konw XDocument could implement this or not. or other class could do this.

Any Suggestions?

Thanks for your all advance!!!

Pravinkumar R. D.
Pravinku...

Contributor

Contributor

4300 points

708 Posts

Answered Question

Re: How to use XDocument to add, update, delete the xml node information?

Hi,

Check the articals below. It may help you out for Add/Delete/Modify xml nodes.

http://www.programminghelp.com/programming/dotnet/using-linq-to-xml-to-add-data-to-xml-file-in-c/

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/creatingsavingxmltreelinq07292009084107AM/creatingsavingxmltreelinq.aspx

Check the samples for LINQ-To-XML

http://msdn.microsoft.com/en-us/vbasic/bb688087.aspx

Thanks,

Pravin

"Please mark as answered, if this answers your question"

spiderman110
spiderma...

Member

Member

225 points

282 Posts

Answered Question

Re: How to use XDocument to add, update, delete the xml node information?

 Thank you very much.
I just find the way how to add, update, delete xml file using XDocument

There are sample source.Hope it is useful to others.

XDocument xd = XDocument.Parse(xmlString);

// add child node of appSettings
xd.Root.Element("appSettings").Add(new XElement("add", new XAttribute("key", "001"),
                                                                           new XAttribute("value", "Value001")));

// delete child node of appSettings
xd.Root.Element("appSettings").RemoveNodes();

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities