Click here to Skip to main content
11,718,589 members (88,162 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#4.0
I have xml file like this

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
        <Name>Mp3 Player</Name>
            <FiveToNine>Old Kannada</FiveToNine>
            <NineToLeven>New Kannada</NineToLeven>
            <LevenToThirteen>New Hindi</LevenToThirteen>
            <ThirteenToFourteen>Old Hindi</ThirteenToFourteen>
            <ForteenToFifteen>Old English</ForteenToFifteen>
            <FifteenToSeventeen>New English</FifteenToSeventeen>
            <SeventeenToEighteen>Old Malayalam</SeventeenToEighteen>
            <EighteenToTwenty>New Malayalam</EighteenToTwenty>
            <TwentyToTwentytwo>Old Tamil</TwentyToTwentytwo>
            <TwentytwoToTwentyfour>New Tamil</TwentytwoToTwentyfour>

Now I want to add a new node list inside node, and I want to update the value of one node inside node.

Can any one help me please?

Thanks in @dv@nce....:P
Posted 5-Jul-13 2:16am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Plain and simple... to change i.e.
var doc = XDocument.Load([FILENAME]);
doc.Root["Body"]["Monday"]["FiveToNine"] = "What ever";
to add
var myNewElement = new XElement("NodeName", "NodeValue");
and save it afterwards...

Maybe you want to take a look at these pages[^]

Sorry, mixed up XmlDocument and XDocument....

static void Main(string[] args) 
    var doc = new XmlDocument();
    //To change
    doc["MediaPlayer"]["Body"]["Name"].InnerText = "Something else";
    //To add
    var newNode = doc.CreateElement("Wednesday");
Aboobakkar Siddeq D.U at 6-Jul-13 1:58am
I'm not able use doc.Root["Body"]["Monday"]["FiveToNine"] = "Something"; System is throwing error like "Cannot apply indexing with [] to an expression of type 'System.Xml.Linq.XElement'",
What is the solution for that?
s_mon at 6-Jul-13 2:14am
Changed the solution... got a little bit mixed up... jst fiddle a little bit around to get a grip on the context.
Aboobakkar Siddeq D.U at 6-Jul-13 2:24am
Now how can I remove that error?
s_mon at 6-Jul-13 2:54am
Just use the code with the XmlDocument...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 750
1 F-ES Sitecore 250
2 OriginalGriff 230
3 Jochen Arndt 160
4 Maciej Los 130
0 Sergey Alexandrovich Kryukov 1,679
1 OriginalGriff 610
2 Maciej Los 529
3 Richard MacCutchan 460
4 F-ES Sitecore 455

Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 6 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100