C# : Modify a xml node

2020-02-10 17:26发布

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.

5条回答
老娘就宠你
2楼-- · 2020-02-10 18:08

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

查看更多
我命由我不由天
3楼-- · 2020-02-10 18:12
XDocument xDoc = XDocument.Load(.....);
xDoc.Descendants("Title").First().Value = "New Value";
xDoc.Save(...)
查看更多
倾城 Initia
4楼-- · 2020-02-10 18:14
XmlDocument xml = new XmlDocument();
xml.Load(...);

var newTitle = "MODIFIED";
var title_node = xml.GetElementsByTagName("Title");
if(!string.IsNullOrEmpty(newTitle) && title_node.Count > 0)
{
    title_node[0].InnerText = newTitle;
}
查看更多
Explosion°爆炸
5楼-- · 2020-02-10 18:19
XDocument doc = XDocument.Load("0.xml");
IEnumerable<XElement> rech =
                     from el in doc.Root.Elements("reminder")
                     where (string)el.Element("Title") == "Alarm1"
                     select el;
if (rech.Count() != 0)
{
   foreach (XElement el in rech)
   {
       el.Element("Title").SetValue("NEW TITLE");
   }
}
doc.Save("0.xml");
查看更多
对你真心纯属浪费
6楼-- · 2020-02-10 18:22

If you want to use linq with xml (I find it the best way) then you will want to use the System.Xml.Linq namespace. The classes in that namespace are all prefixed with just X not Xml. The functionality in this namespace is newer, better and much easier to manipulate with Linq.

var xml = XDocument.Load("0.xml");
var alarm1 = xml.Descendants("reminder")
                .Single(r => r.Element("Title") == "Alarm1");

This code will give you a variable, alarm1 that is the reminder that has a title node of "Alarm1."

From that point its not clear to me exactly what you want to modify. If you just want to change the title then ...

alarm1.Element("Title").Value = "MODIFIED";
xml.Save("0.xml");
查看更多
登录 后发表回答