XmlDocument CreateXmlDeclaration()- How to use CreateXmlDeclaration() method
CreateXmlDeclaration.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html>
<script runat="server">
void Button1_Click(object sender, System.EventArgs e)
{
string xmlPath = Request.PhysicalApplicationPath + @"App_Data\BookList1.xml";
if (System.IO.File.Exists(xmlPath))
{
Label1.Text = "Xml Document Already Exists";
}
else
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode declarationNode = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmlDoc.AppendChild(declarationNode);
XmlNode booksNode = xmlDoc.CreateElement("books");
xmlDoc.AppendChild(booksNode);
XmlNode bookNode = xmlDoc.CreateElement("book");
XmlNode bookNameNode = xmlDoc.CreateElement("bookname");
bookNameNode.InnerText = ".NET Domain-Driven Design with C#: Problem - Design - Solution";
bookNode.AppendChild(bookNameNode);
XmlNode priceNode = xmlDoc.CreateElement("price");
priceNode.InnerText = "39.99";
bookNode.AppendChild(priceNode);
booksNode.AppendChild(bookNode);
xmlDoc.Save(xmlPath);
Label1.Text = "Xml Document Created Successfully!";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>XmlDocument CreateXmlDeclaration()- How to use CreateXmlDeclaration() method</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:HotPink; font-style:italic;">XmlDocument Example: Using CreateXmlDeclaration() Method</h2>
<hr width="675" align="left" color="Pink" />
<br /><br />
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
Text=""
ForeColor="SeaGreen"
Font-Names="Comic Sans MS"
Font-Size="X-Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Create XmlDocument Using XmlDocument"
Height="42"
Font-Bold="true"
ForeColor="SkyBlue"
/>
</div>
</form>
</body>
</html>



- asp.net and XPath: evaluate [position()<5] XPath expression
- asp.net and XPath: evaluate [position()>4] XPath expression
- asp.net and XmlDocument - Load xml document from stream (file) datasource
- asp.net and XmlDocument - Load XmlDocument from string variable and save file
- XmlDocument - Load XmlDocument from XmlReader and use XPath
- XmlDocument- How to create xml document programmatically using XmlDocument
- XmlDocument CreateElement()- How to create element using XmlDocument in asp.net
- XmlDocument CreateAttribute() - How to create attribute using XmlDocument