Is it possible to add attributes with XmlTextWriter?
In this short example we will see whether you can add attributes to the XML file, so that the XML file looks like in the previous examples. The XML file which we used in the previous example looked like this.
XmlTextWriter.xml: (without attributes)
<CategoryList>
<Category>
<MainCategory>XML</MainCategory>
<Description>XML Articles</Description>
<Active>Yes</Active>
</Category>
</CategoryList>
If you have read the previous examples then you have recognized that I am using the following XML.
XmlTextWriter.xml: (with attributes)
<CategoryList>
<Category>
<MainCategory ID="01">XML</MainCategory>
<Description>XML Articles</Description>
<Active>Yes</Active>
</Category>
</CategoryList>
The key to add the attributes is to use the function WriteAttributeString(). This function takes two parameters: The first one is the attribute name and the second one is the attribute value. However you can not call this function after WriteElementString. The XmlTextWriter class doesn't allow that. If you still do this, you will get the following error message:
Unhandled Exception: System.InvalidOperationException:
Token StartAttribute in state Content would result in an invalid XML document.
So the answer to the question is yes, you can add attributes to the XML document, but the XmlTextWriter class won't create the XML file which we need. The XmlTextWriter will only let you add the attribute after calling WriteStartElement(). The code self contains comments which will explain you the steps to create the XML file.
XmlTextWriterWithAttribute.aspx:
<%@ import namespace="System.Xml" %>
<%@ page language="C#" debug="true"%>
<script runat="server">
void page_load(object sender, System.EventArgs e){
XmlTextWriter xmlWriter = new XmlTextWriter(Server.MapPath("XmlTextWriterWithAttribute.xml"), null);
// Format the xml automatically to a tree structure
xmlWriter.Formatting = Formatting.Indented;
// Write root element
xmlWriter.WriteStartElement("CategoryList");
// Write first sub element of root element
xmlWriter.WriteStartElement("Category");
// Write ID Attribute
// First parameter is the attribute name and the second is the value
xmlWriter.WriteAttributeString("ID","0");
// now start adding elements to the Category element
xmlWriter.WriteElementString("MainCategory","XML");
// add the 2nd element to the Category element
xmlWriter.WriteElementString("Description","XML Articles");
// add the 3rd element to the Category element
xmlWriter.WriteElementString("Active","Yes");
// write end elements
xmlWriter.WriteEndElement();
// close writer
xmlWriter.Close();
Response.Write("Xml Written");
}
</script>
Created XML file:
<CategoryList>
<Category ID="0">
<MainCategory>XML</MainCategory>
<Description>XML Articles</Description>
<Active>Yes</Active>
</Category>
</CategoryList>
As you can see this is not really what I want. I want the attribute to be added to the MainCategory element. However as mentioned the XmlTextWriter doesn't allow you to do this.