4

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?

8
  • 1
    LoadXml is for loading an xml document represented as text. You want to use Load instead (as in the commented line) Commented Nov 2, 2012 at 9:18
  • Root element is missing this error occures now Commented Nov 2, 2012 at 9:20
  • 1
    Means your document is empty. Just do a new XmlDocument() then Commented Nov 2, 2012 at 9:30
  • 1
    Like Simon said, Load is 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. Commented Nov 2, 2012 at 9:36
  • @Sudheesh you xml file is empty..have you checked whether the xml is empty or contain the same format as shown above..check the xml file and the path you are referring to.. Commented Nov 2, 2012 at 9:41

3 Answers 3

1

Try using XML Serialization:

public static partial class ObjectXMLSerializer<T> where T : class
{
            private static void SaveToDocumentFormat(T serializableObject, System.Type[] extraTypes, string path, IsolatedStorageFile isolatedStorageFolder)
            {
                using (TextWriter textWriter = CreateTextWriter(isolatedStorageFolder, path))
                {
                    XmlSerializer xmlSerializer = CreateXmlSerializer(extraTypes);

                    //Cuong: set name space to null
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    xmlSerializer.Serialize(textWriter, serializableObject, ns);
                }
            }
            public static void Save(T serializableObject, string path)
            {
                    SaveToDocumentFormat(serializableObject, null, path, null);
            }

}
Sign up to request clarification or add additional context in comments.

Comments

0

When you load your Xml you need to use Server.MapPath . XmlDoc.LoadXml(Server.MapPath("Password.xml"));

Comments

0

delete

<?xml version="1.0" encoding="utf-8"?>

from

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
  <UserName>abc</UserName>
  <PassWord>123</PassWord>
</ROOT>

I do not know why it works but we do that all the time in our code. And yes you should be using XmlDocument.Load not XmlDocument.LoadXml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.