Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have xml file like this

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<MediaPlayer>
    <Body>
        <Name>Mp3 Player</Name>
        <Days>
        <Monday>
            <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>
            <ZeroToFive>Arabic</ZeroToFive>
        </Monday>

        <Tuesday>
            <FiveToNine>AAAAAA</FiveToNine>
            <NineToLeven>BBBBBBB</NineToLeven>
            <LevenToThirteen>CCCCCCCC</LevenToThirteen>
            <ThirteenToFourteen>DDDDDDDD</ThirteenToFourteen>
            <ForteenToFifteen>EEEEEEE</ForteenToFifteen>
            <FifteenToSeventeen>FFFFFFF</FifteenToSeventeen>
            <SeventeenToEighteen>GGGGGGG</SeventeenToEighteen>
            <EighteenToTwenty>HHHHHHH</EighteenToTwenty>
            <TwentyToTwentytwo>IIIIIIIIII</TwentyToTwentytwo>
            <TwentytwoToTwentyfour>JJJJJJJJJJJ</TwentytwoToTwentyfour>
            <ZeroToFive>KKKKKKKK</ZeroToFive>
        </Tuesday>

    </Days>
  </Body>
</MediaPlayer>


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

Can any one help me please?

Thanks in @dv@nce....:P
Posted

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

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

---------------------------
Sorry, mixed up XmlDocument and XDocument....

C#
static void Main(string[] args) 
{

    var doc = new XmlDocument();

    doc.Load(@"MyFile.xml");

    //To change
    Console.WriteLine(doc["MediaPlayer"]["Body"]["Name"].InnerText);
    
    doc["MediaPlayer"]["Body"]["Name"].InnerText = "Something else";

    Console.WriteLine(doc["MediaPlayer"]["Body"]["Name"].InnerText);

    //To add
    var newNode = doc.CreateElement("Wednesday");
    doc["MediaPlayer"]["Body"].AppendChild(newNode);

    Console.WriteLine(doc["MediaPlayer"]["Body"].InnerXml);

    Console.WriteLine("Done...");
    Console.ReadLine();

}
 
Share this answer
 
v3
Comments
Aboobakkar Siddeq D U 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?
StM0n 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 6-Jul-13 2:24am    
Now how can I remove that error?
StM0n 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900