Tuesday 18 June 2013

.Net Tips : Create or Generate XML file programmatically using XElement and XAttribute class with C#.Net and VB.Net example

We can Create or Generate XML file or document programmatically using XDocument, XElement and XAttribute class. We can use infinite level of XML element. We can also create "web.config" file programmatically.

Here is example for this.
In this example we create sample "web.config" file using XElement and XAttribute class and display that generated XML in textbox. We are using ".ToString" method of XDocument object to get XML string.

ASPX Code :
    <asp:TextBox runat="server" ID="txtXML" TextMode="MultiLine" Rows="20" Columns="110"></asp:TextBox>


C#. Net Example :
        XDocument myDocument = new XDocument(
                                  new XElement("configuration",
                                    new XElement("system.web",
                                      new XElement("membership",
                                        new XElement("providers",
                                          new XElement("add",
                                            new XAttribute("name","WebAdminMembershipProvider"),
                                            new XAttribute("type","System.Web.Administration.WebAdminMembershipProvider")))),
                                      new XElement("httpModules",
                                        new XElement("add",
                                          new XAttribute("name","WebAdminModule"),
                                          new XAttribute("type","System.Web.Administration.WebAdminModule"))),
                                      new XElement("authentication",
                                        new XAttribute("mode", "Windows")),
                                      new XElement("authorization",
                                        new XElement("deny",
                                          new XAttribute("users", "?"))),
                                      new XElement("identity",
                                        new XAttribute("impersonate", "true")),
                                      new XElement("trust",
                                        new XAttribute("level", "full")),
                                      new XElement("pages",
                                        new XAttribute("validationRequest", "true")))));

        txtXML.Text = myDocument.ToString();

VB.Net Examples :
        Dim myDocument As New XDocument(
            New XElement("configuration",
                                    New XElement("system.web",
                                        New XElement("membership",
                                        New XElement("providers",
                                            New XElement("add",
                                            New XAttribute("name", "WebAdminMembershipProvider"),
                                            New XAttribute("type", "System.Web.Administration.WebAdminMembershipProvider")))),
                                        New XElement("httpModules",
                                        New XElement("add",
                                            New XAttribute("name", "WebAdminModule"),
                                            New XAttribute("type", "System.Web.Administration.WebAdminModule"))),
                                        New XElement("authentication",
                                        New XAttribute("mode", "Windows")),
                                        New XElement("authorization",
                                        New XElement("deny",
                                            New XAttribute("users", "?"))),
                                        New XElement("identity",
                                        New XAttribute("impersonate", "true")),
                                        New XElement("trust",
                                        New XAttribute("level", "full")),
                                        New XElement("pages",
                                        New XAttribute("validationRequest", "true")))))

        txtXML.Text = myDocument.ToString()

Output :
Create or Generate XML file programmatically using XElement and XAttribute class
(To view original image , click on image)

1 comment:

  1. regular reader...excepting more tips.




    ReplyDelete