Question

I have been trying to write textbox value to xml file but could not get it in right format as I wish to have

I am using following code

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", txtUserId.Text),
             new XAttribute("Password", txtPwd.Text));

if (Xdoc.Descendants().Count() > 0)
    Xdoc.Descendants().First().Add(xml);
else
    Xdoc.Add(xml);

Xdoc.Save("D:\\Users.xml");

Here i m getting xml in this format

<User UserId="Sunny" Password="Sunny">
<User UserId="Sunny" Password="Sunny" />
<User UserId="Sunny" Password="Sunny" />
</User>

But i wanna to have like this

<Users>
  <User>
    <UserId>Sunny</UserId>
    <Password>pwd</Password>
  </User>
  <User>
    <UserId>Sunny</UserId>
    <Password>pwd</Password>
  </User>
</Users>
Was it helpful?

Solution 2

My own answer

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 xmlstart = new XElement("Users");
            Xdoc.Add(xmlstart);
        }
        XElement xml = /*new XElement("Users",*/
                       new XElement("User",
          new XElement("UserId", txtUserId.Text),
          new XElement("Password", txtPwd.Text));

        if (Xdoc.Descendants().Count() > 0)
            Xdoc.Descendants().First().Add(xml);
        else
        {
            Xdoc.Add(xml);
        }

        Xdoc.Element("Users").Save("D:\\Users.xml");

This is giving me xml like

<?xml version="1.0" encoding="utf-8"?>
<Users>
  <User>
  <UserId>Sunny</UserId>
  <Password>Sunny</Password>
 </User>
 <User>
   <UserId>Sunny</UserId>
   <Password>Sunny</Password>
 </User>
 <User>
   <UserId>Sunny</UserId>
   <Password>Sunny</Password>
 </User>
</Users>

OTHER TIPS

Then use XElement instead of XAttribute.

 new XElement("User",
              new XElement("UserId", txtUserId.Text),
              new XElement("Password", txtPwd.Text));

And to add multiple users, given that you have userList:

 new XElement("Users",
        userList.Select(u=>
             new XElement("User",      
                 new XElement("UserId", u.UserId),
                 new XElement("Password", u.Password)));

If you want the values to appear as elements then you should use XElement instead of XAttribute...

E.g.

XElement xml = /*new XElement("Users",*/
                   new XElement("User",
                   new XElement("UserId", "sunny"),
                   new XElement("Password", "pwd")
                   );

To add these elements under the root use:

Xdoc.Element("Users").Add(xml);

It just changed from XAttribute to XElement. Because attributes would be added inside the element like . Elements play a different role which form a tree XML structure.

The Root element is now become Users as per your requirement.

XElement xml = new XElement("Users",   
                       new XElement("User",
                       new XElement("UserId", "sunny"),
                       new XElement("Password", "pwd")
                       ));

If you want repeatedly add the child element use the below code block either individual or in loop.

xml.Add(
new XElement("User", new XElement("UserId", "sunny"), new XElement("Password", "pwd") ) );

Refer your complete sample

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 XElement("UserId", "sunny"),
                       new XElement("Password", "pwd")
                       );

        if (Xdoc.Descendants().Count() > 0)
            Xdoc.Descendants().First().Add(xml);
        else
        {
            Xdoc.Add(xml);
        }

        Xdoc.Save("D:\\Users.xml");

Give a try for this,

    var users= new XElement("Users");

    var userXml= new XElement("User",
                   new XElement("UserId", txtUserId.Text),
                   new XElement("Password", txtPwd.Text)
                   );

    users.Add(userXml);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top