0

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>
0

5 Answers 5

1

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)));
Sign up to request clarification or add additional context in comments.

5 Comments

How can i add Users as root element ??
I dont have any userlist with me. I just wanna know how i add Users at the top of xml and then add user as child of this root node
Time to notice the pattern in using XElement :) new XElement("Users", new XElement("User", new XElement("UserId", u.UserId), new XElement("Password", u.Password)))
Here i want to have Users only one time as root element. This one adding Users every time i call this code
Thanks alex you have given me nice idea how can i get this one. I have posted my own answer which works as expected..
1

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>

Comments

0

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);

Comments

0

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");

1 Comment

How can i add users as root element.Please have a look at edited question again
0

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);

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.