Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Modify a xml node

i have that xml file :

<?xml version="1.0" encoding="utf-8"?>
<reminders>
  <reminder>
    <Title>Alarm1</Title>
    <Description>Desc1</Description>
    <Time>03/07/2012 10:11AM</Time>
    <snooze>1</snooze>
    <repeat>None</repeat>
  </reminder>
</reminders>

And i want to modify the innertext from Alarm1 to another value so i wrote that code which actually duplicate the entire node .

        XmlDocument xml = new XmlDocument();

        xml.Load("0.xml");

        XmlNodeList elements = xml.SelectNodes("//reminders");

        foreach (XmlNode element in elements)
        {
            if (element.InnerText == "Alarm1")
            {
                XmlNode newvalue = xml.CreateElement("MODIFIED");
                element.ReplaceChild(newvalue, element);

                xml.Save("0.xml");
            }
        }

And then tried another code :

        foreach (XmlElement element in xml.SelectNodes("//reminder"))
        {
            if (element.InnerText == "Alarm1")
            {
                XmlNode newvalue = xml.CreateElement("MODIFIED");
                element.ReplaceChild(newvalue, element);

                xml.Save("0.xml");
            }
        }

But also doesn`t work....

EDIT 1 : [Figured out a new code]

        XmlDocument xml = new XmlDocument();

        xml.Load("0.xml");

        foreach (XmlElement element in xml.SelectNodes("//reminder"))
        {
            foreach (XmlElement element1 in element)
            {
                if (element.SelectSingleNode("//Title").InnerText == "Alarm1")
                {
                    XmlNode newvalue = xml.CreateElement("MODIFIED");
                    element.ReplaceChild(newvalue, element1);

                    xml.Save("0.xml");
                }
            }
        }

But it made the Alarm1 becomes

<MODIFIED />

EDIT 2 : [I SOLVED IT :D] Okay here is the code i could figure out :

        XmlDocument xml = new XmlDocument();

        xml.Load("0.xml");

        foreach (XmlElement element in xml.SelectNodes("//reminder"))
        {
            foreach (XmlElement element1 in element)
            {
                if (element.SelectSingleNode("//Title").InnerText == "Alarm1")
                {
                    MessageBox.Show(element1.InnerText);
                    XmlNode newvalue = xml.CreateElement("Title");
                    newvalue.InnerText = "MODIFIED";
                    element.ReplaceChild(newvalue, element1);

                    xml.Save("0.xml");
                }
            }
        }

I`ll really appreciate your helps and thanks.

like image 202
BOSS Avatar asked Mar 08 '12 10:03

BOSS


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

Try this:

xml.SelectSingleNode("//reminder/Title").InnerText = "NewValue";

Your foreach line is simply looping through a list of elements called "reminders", not it's child nodes.

Take a look at this xpath tutorial for more information:

http://www.w3schools.com/xpath/xpath_intro.asp

like image 101
aaroncatlin Avatar answered Sep 22 '22 02:09

aaroncatlin