I'm trying to store Some values to an xml file.I've already created an Xml file and trying to overwrite data. The code is given..
/*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; }
}
on button click..
NewPassword nps = new NewPassword();
nps.username = TxtUser.Text;
nps.Password = TxtNewPassword.Text;
StorePassword sp=new StorePassword();
sp.store(nps);
The existing Xml file contains the following..
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<UserName>abc</UserName>
<PassWord>123</PassWord>
</ROOT>
But it's not working..
Data at the root level is invalid. Line 1, position 1
this error occures..
Ichanged the code as XmlDoc.Load(@"Password.xml");
now error changed to
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.
why this happens?
new XmlDocument()thenLoadis the method you should use. Can you post the full error message you get & where you get it? Also, try to take a look at this question.