我有兩個文本框,分別是txtUserid和txtPassowrd。我正在將文本框中輸入的值寫入xmlfile,但我不希望相同的txtuserid值在xml中寫入兩次 - 它應該被覆蓋;即如果我輸入txtUserid = 2和txtPassword = I,第二次如果我輸入txtUserid = 2和txtPassword = m,那麼我只想在xml中輸入一個條目...即txtUserid = 2和textPassword = m將文本框值寫入C#中的xml文件.net
是我的代碼
XDocument Xdoc = new XDocument(new XElement("Users"));
if (System.IO.File.Exists("D:\\Users.xml"))
Xdoc = XDocument.Load("D:\\Users.xml");
else
Xdoc = new XDocument();
XElement xml = new XElement("Users",
new XElement("User",
new XAttribute("UserId", txtUserName.Text),
new XAttribute("Password", txtPassword.Text)
));
if (Xdoc.Descendants().Count() > 0)
Xdoc.Descendants().First().Add(xml);
else
{
Xdoc.Add(xml);
}
Xdoc.Save("D:\\Users.xml");
不要以純文本形式將密碼保存在xml文件中。 – 2012-01-16 16:04:53
目前我不能做代碼細節,但最簡單的方法是在創建新代碼之前刪除任何現有的用戶節點。 – 2012-01-16 16:06:14