hello !
i want to make a xml file to store my Settings such as server name , user name , and password , etc , i try to search how to read and write xml file i found this code for writing an xml file it works fine for me

private void XMLWrite()
        {
            FileStream fs = new FileStream("c:\\Server.xml", FileMode.Create);

            XmlWriter w = XmlWriter.Create(fs);

            w.WriteStartDocument();
            w.WriteStartElement("Configuration");

            // Write a product.
            w.WriteStartElement("Parameters");
            w.WriteAttributeString("servername", txtservername .Text );
            w.WriteElementString("username", txtusername.Text );
            w.WriteElementString("password",txtpassword.Text );
            w.WriteEndElement();
            w.WriteEndDocument();
            w.Flush();
            fs.Close();
        }

now i have a prob , and prob is that ,i want to read it and show all these values in textboxes present on my form .for example , txtservername , txtusername, txtpassword.
please any one guide me to solve this issue .

Best Regards

M.Waqas Aslam

Hi..
you can try..

StreamReader reader = File.OpenText("c:\\path");
string input = null;
while ((input = reader.ReadLine()) != null)
{
  Console.WriteLine(input);
}

thanks for ur help kamilacbe , but its not performing the task i want to perform , in my xml file there is 3 attributes .
<server>servername</server>
<username>username</username>
<password>password</password>
i want to get jus servername , username , password , and want to show them in my textboxes name , txtservername , txtusername, txtpassword ,:(
can u please help me in this ?

Regards
M.Waqas Aslam

Use Linq to Xml (import the System.Xml.Linq namespace). That way you can easily read/write XML document.

To read Xml document.

doc = XDocument.Load(file);

string server = doc.Root.Element("Parameters").Element("servername").Value;
string username = doc.Root.Element("Parameters").Element("username").Value;
Console.WriteLine(server);
Console.WriteLine(username);

To create Xml document.

XDocument doc = new XDocument();
XElement root=new XElement("Configuration");
XElement param=new XElement("Parameters");
param.Add(new XElement("servername","A"));
param.Add(new XElement("username","B"));
param.Add(new XElement("password","C"));

root.Add(param);
doc.Add(root);
doc.Save(file);

I did something very similar were i exported the properties of an object to Xml and then imported them back to a property grid instead. I would suggest that you had some structure to you Xml document (e.g. make ServerName the parent node with UserName being a child node and password being a child node of UserName. Here is the code i used for importing the properties.

try
            {
                XmlDocument config = new XmlDocument();
                config.Load("My_Xml_Doc_Path");

                // Creates instance of all node within loaded Xml docuemnt and retrieves first element by tag name
                XmlNodeList ServerName = config.GetElementsByTagName("ServerName");

                //Loops for each child node within ServerName node 
                foreach (XmlNode n in ServerName)
                {
                    //Converts XmlNode to XmlElement
                    XmlElement _ServerName = (XmlElement)n;
                    //Converts and stroes value of specified attribute in property of ObjectTypeState class
                    serverNameTextBox.txt = _ServerName.GetAttribute("ServerName").ToString();

                }

                XmlNodeList UserName = config.GetElementsByTagName("UserName");
                foreach (XmlNode n in UserName)
                {
                    XmlElement _UserName = (XmlElement)n;
                    UserNameTextBox.txt = _UserName.GetAttribute("UserName").ToString();
                }

                XmlNodeList Password = config.GetElementsByTagName("Password");
                foreach (XmlNode n in Password)
                {
                    XmlElement _Password = (XmlElement)n;
                    PasswordTextBox.txt = _Password.GetAttribute("Password").ToString();
                }
            }

I hope this helps although i'm not sure it's completely correct as i modified my original code to fit your request as mine was too long.

This question has already been answered. Start a new discussion instead.