C# 如何更新 XML 节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/482986/
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
How to Update a XML Node?
提问by balexandre
It is easy to read an XML file and get the exact Node Text, but how do I Update that Node with a new value?
读取 XML 文件并获取确切的节点文本很容易,但是如何使用新值更新该节点?
To read:
阅读:
public static String GetSettings(SettingsType type, SectionType section)
{
XmlTextReader reader = new XmlTextReader(HttpContext.Current.Request.MapPath(APPSETTINGSPATH));
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNode node = document.SelectSingleNode(
String.Format("/MyRootName/MySubNode/{0}/{1}",
Enum.Parse(typeof(SettingsType), type.ToString()),
Enum.Parse(typeof(SectionType), section.ToString())));
return node.InnerText;
}
to write ...?
来写 ...?
public static void SetSettings(SettingsType type, SectionType section, String value)
{
try
{
XmlTextReader reader = new XmlTextReader(HttpContext.Current.Request.MapPath(APPSETTINGSPATH));
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNode node = document.SelectSingleNode(
String.Format("/MyRootName/MySubNode/{0}/{1}",
Enum.Parse(typeof(SettingsType), type.ToString()),
Enum.Parse(typeof(SectionType), section.ToString())));
node.InnerText = value;
node.Update();
}
catch (Exception ex)
{
throw new Exception("Error:", ex);
}
}
Notethe line, node.Update(); does not exist, but that's what I wanted :)
注意这一行,node.Update(); 不存在,但这就是我想要的:)
I saw the XmlTextWriter object, but it will write the entire XML to a new file, and I just need to update one value in the original Node, I can save as a new file and then rename the new file into the original name but... it has to be simpler to do this right?
我看到了 XmlTextWriter 对象,但它会将整个 XML 写入一个新文件,而我只需要更新原始 Node 中的一个值,我可以另存为新文件,然后将新文件重命名为原始名称。 ..这样做必须更简单吗?
Any of you guys have a sample code on about to do this?
你们中的任何人都有关于要执行此操作的示例代码吗?
Thank you
谢谢
采纳答案by Jon Skeet
You don't need an "update" method - setting the InnerText property updates it. However, it only applies the update in memory. You doneed to rewrite the whole file though - you can't just update a small part of it (at least, not without a lotof work and no out-of-the-box support).
您不需要“更新”方法 - 设置 InnerText 属性会更新它。但是,它只在内存中应用更新。不过,您确实需要重写整个文件 - 您不能只更新其中的一小部分(至少,不是没有大量工作并且没有开箱即用的支持)。
回答by AnthonyWJones
XmlDocument.Load
has an overload that will take the filename directly so there is no need for the reader.
XmlDocument.Load
有一个将直接获取文件名的重载,因此不需要读者。
Similarly when you are done XmlDocument.Save
will take a filename to which it will save the document.
同样,当您完成XmlDocument.Save
后将取一个文件名,它将保存文档。
回答by axel_c
You're updating the node in an in-memoryrepresentation of the xml document, AFAIK there's no way to update the node directly in the physical file. You have to dump it all back to a file.
您正在xml 文档的内存表示中更新节点,AFAIK 无法直接在物理文件中更新节点。您必须将其全部转储回文件。
回答by EvilInside
The nodeValue property can be used to change the value of a text node.
nodeValue 属性可用于更改文本节点的值。
The following code changes the text node value of the first element: Example:
以下代码更改第一个元素的文本节点值: 示例:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";