讀取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