读取XML文件并获取确切的节点文本非常容易,但是如何使用新值更新该节点?如何更新XML节点?
阅读:
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;
}
写的......?
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);
}
}
注行,node.Update();不存在,但这就是我想要的:)
我看到了XmlTextWriter对象,但它会将整个XML写入一个新文件,并且我只需要在原始Node中更新一个值,我可以另存为一个新的文件,然后将新文件重命名为原始名称,但是......它必须更简单才能做到这一点?
你们中的任何一个人都有一个示例代码即将做到这一点?
谢谢
无论如果我使用读取器或过载的方法,我将最终具有:“该进程无法访问文件'C:\ MyWebsite \ appAuthentication.xml',因为它正在被另一个进程使用。”当我使用document.WriteTo(writer)时, – balexandre 2009-01-27 11:07:59
甚至document.Save(作家); – balexandre 2009-01-27 11:09:01