开发者

How to create xml in asp.net?

I have a web form that contains开发者_C百科 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?


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.


    /// <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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜