C#:修改一个xml节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9616163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# : Modify a xml node
提问by BOSS
i have that xml file :
我有那个 xml 文件:
<?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 .
我想将 Alarm1 中的内部文本修改为另一个值,所以我编写了实际复制整个节点的代码。
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]
编辑1:[想出一个新代码]
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
但它使Alarm1变成
<MODIFIED />
EDIT 2 : [I SOLVED IT :D] Okay here is the code i could figure out :
编辑 2:[我解决了它:D] 好的,这是我能弄清楚的代码:
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.
我真的很感激你的帮助和感谢。
采纳答案by aaroncatlin
Try this:
尝试这个:
xml.SelectSingleNode("//reminder/Title").InnerText = "NewValue";
Your foreachline is simply looping through a list of elements called "reminders", not it's child nodes.
您的foreach行只是遍历称为“提醒”的元素列表,而不是子节点。
Take a look at this xpath tutorial for more information:
查看此 xpath 教程以获取更多信息:
回答by Julien
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");
回答by Jodrell
If you want to use linq with xml (I find it the best way) then you will want to use the System.Xml.Linqnamespace. The classes in that namespace are all prefixed with just Xnot Xml. The functionality in this namespace is newer, better and much easier to manipulate with Linq.
如果您想将 linq 与 xml 一起使用(我认为这是最好的方法),那么您将需要使用System.Xml.Linq命名空间。该命名空间中的类都以Xnot为前缀Xml。此命名空间中的功能更新、更好且更易于使用 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, alarm1that is the reminder that has a title node of "Alarm1."
此代码将为您提供一个变量,alarm1即标题节点为“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");
回答by L.B
XDocument xDoc = XDocument.Load(.....);
xDoc.Descendants("Title").First().Value = "New Value";
xDoc.Save(...)
回答by Bharat
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;
}

