Click here to Skip to main content
Rate this: bad
good
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" ?>
<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 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 3:16am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
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");
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....
 
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();
 
}
  Permalink  
v3
Comments
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 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 6 Jul 2013
Copyright © CodeProject, 1999-2014
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