WriteAttributeString() Method - Write attribute in XML file
WriteAttribute.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlFile = Request.PhysicalApplicationPath + @"App_Data\BookStore2.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.OmitXmlDeclaration = false;
try
{
using (XmlWriter writer = XmlWriter.Create(xmlFile, settings))
{
writer.WriteStartDocument(false);
writer.WriteComment("Sample file for test WriteAttributeString method.");
writer.WriteStartElement("books");
writer.WriteStartElement("book");
writer.WriteAttributeString("id", "001"); ;
writer.WriteElementString("name", "Learn Adobe Photoshop CS4 by Video");
writer.WriteElementString("author", "Video2Brain");
writer.WriteElementString("price","$59.99");
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
Label1.Text = "File written successfully!";
}
}
catch (Exception ex)
{
Label1.Text = "An Error Occured: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>WriteAttributeString: How to write attribute in xml file in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">XML Example: Writing Attributes In Xml File</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="false"
ForeColor="HotPink"
Font-Size="Large"
Font-Names="Comic Sans MS"
>
</asp:Label>
</div>
</form>
</body>
</html>


- A Sample Xml File source code for various asp.net Xml example
- XmlReader: How to read and process Xml file element name in asp.net
- XmlReader HasAttributes and GetAttribute: How to read attribute in asp.net Xml
- XmlReaderSettings: How to use XmlReaderSettings with XmlReader in asp.net Xml
- WriteComment: How to write comment in xml file in asp.net
- How to use WriteStartElement, WriteEndElement and WriteElementString method in asp.net Xml
- XmlWriter: How to write Xml file programmatically in asp.net