我正在尝试将一些值存储到 xml 文件中。我已经创建了一个 Xml 文件并尝试覆盖数据。代码给了..
/*storepassword.cs *//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
public class StorePassword
{
public StorePassword()
{
}
public void store(NewPassword nps)
{
XmlDocument XmlDoc = new XmlDocument();
//XmlDoc.Load(@"Password.xml");
XmlDoc.LoadXml("Password.xml");
XmlNode root = XmlDoc.DocumentElement;
XmlNode myNode1 = root.SelectSingleNode("UserName");
XmlNode myNode2 = root.SelectSingleNode("PassWord");
myNode1.Value = "sjn";
myNode2.Value = "sjn123";
XmlDoc.Save(@"Password.xml");
}
}
//NewPassword.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class NewPassword
{
public NewPassword()
{
}
public string username{ get; set; }
public string Password{ get; set; }
}
在按钮点击..
NewPassword nps = new NewPassword();
nps.username = TxtUser.Text;
nps.Password = TxtNewPassword.Text;
StorePassword sp=new StorePassword();
sp.store(nps);
现有的 Xml 文件包含以下内容。
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<UserName>abc</UserName>
<PassWord>123</PassWord>
</ROOT>
但它不工作..
根级别的数据无效。第 1 行,位置 1
出现这个错误。。
我将代码更改为XmlDoc.Load(@"Password.xml");
现在错误更改为
Root element is missing.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XmlException: Root element is missing.
为什么会这样?