Write XML File with namespace : XML File Save « XML « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » XML » XML File Save 
Write XML File with namespace

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = MapPath("EmployeesNew.xml");
        //string xmlFilePath = @"C:\Data\Employees.xml";
        try
        {
            using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
            {
                writer.WriteStartDocument(false);
                writer.WriteStartElement("employees");
                //Write the Namespace prefix for the root element
                writer.WriteAttributeString("xmlns""emp", null, "urn:employees-java2s");
                writer.WriteStartElement("employee""urn:employees-java2s");
                /* You can also use this approach to declare the namespace
                string prefix = writer.LookupPrefix("urn:employees-java2s");
                writer.WriteStartElement(prefix, "employee", null);
                */
                    writer.WriteAttributeString("id""1");
                        writer.WriteStartElement("name""urn:employees-java2s");
                            writer.WriteElementString("firstName""urn:employees-java2s""Nancy");
                            writer.WriteElementString("lastName""urn:employees-java2s""lastName");
                        writer.WriteEndElement();
                        writer.WriteElementString("city""urn:employees-java2s""Seattle");
                        writer.WriteElementString("state""urn:employees-java2s""WA");
                        writer.WriteElementString("zipCode""urn:employees-java2s""98122");
                    writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
                //Flush the object and write the XML data to the file
                writer.Flush();
                lblResult.Text = "File is written successfully";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = "An Exception occurred: " + ex.Message;
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Writing XML File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label id="lblResult" runat="server" />
    </div>
    </form>
</body>
</html>

           
       
Related examples in the same category
1.Save updated XML data set to new XML file
2.Save XML data to String Writer
3.Load XML from String and write to file
4.XML File write: element, comments
5.XML file write: Indent, indent char, and Omit Xml Declaration
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.