我試圖將一些值存儲到一個xml文件中。我已經創建了一個Xml文件並嘗試覆蓋數據。該代碼被賦予..使用Asp-C寫入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
此錯誤occures ..
Ichanged代碼爲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.
爲什麼出現這種情況?
LoadXml用於加載一個XML文檔表示爲文本。你想使用加載代替(如在註釋行) –
k ..讓我試試.. – Sudix
根元件丟失 現在發生此錯誤 – Sudix