Surya sasidhar
Surya sasidhar

Reputation: 30293

How to create xml in asp.net?

I have a web form that contains a username, password and an address (textboxes). My intention is, when I click on the insert-button, to convert that to XML.

Can you help me?

Upvotes: 0

Views: 652

Answers (2)

scripni
scripni

Reputation: 2164

    /// <summary>
    /// Returns an xml containing a user formatted like
    ///  <user username="..." password="..." address="..."></user>
    /// </summary>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="address"></param>
    /// <returns></returns>
    public string ConvertUserToXml(string  username, string password, string address)
    {
        var xdoc = new XDocument();
        var user = new XElement("user");
        user.Add(new XAttribute("username", username));
        user.Add(new XAttribute("password", password));
        user.Add(new XAttribute("address", address));
        xdoc.Add(user);
        return xdoc.ToString();
    }

If you want to get just the wrapped user (not an entire xml document), return user.ToString() instead (but note that it will not be a valid xml document in itself).

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

If you already have some object model you could use a XmlSerializer to directly serialize it to XML or XDocument if you want to generate the XML manually.

Upvotes: 2

Related Questions