BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


.NET and XML
How to Write a Comment to a XML File with XmlTextWriter and WriteComment?
How to add the XML Declaration with XmlTextWriter and WriteStartDocument?
How to use Indentation in XmlTextWriter?
What is the usage of WriteFullEndElement and how do I use?
How to include a CData block with XmlTextWriter and WriteCData?
How to write raw XML with XmlTextWriter?
How to add a namespace with the XmlTextWriter and WriteStartAttribute?
How to create a new xml file with XmlDocument?
How to create a xml on the fly?
How to use the Item property in XmlTextReader?
How to Create a Comment with XmlDocument and XmlComment?
How to Read a XML from a Stream with XmlTextReader?
How to Read a XML from a URL with the XmlTextReader?
How to auto-increment an unique ID in XML with XmlTextReader?
How to read Nodes with XmlNodeReader?
How to Get the Attribute Value with XmlNodeReader and GetAttribute?
How to skip child nodes with XmlNodeReader and Skip?
How to convert the return value from XML with XmlConvert and ToInt32?
What is the 'pull model'?
What is EXSLT.NET?


 
 

<< XSLT 


By Sonu Kapoor
First Posted 02/14/2005
Times viewed 224

Is it possible to add attributes with XmlTextWriter?


Summary Is it possible to add attributes with XmlTextWriter?

In the previous example of XmlTextWriter you have seen how you can create a simple xml file. However the elements in the xml file doesn't contain any attributes like we are used from other examples in this library. 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 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. In another example I will show you how you can add the attribute to the MainCategory element. The XmlTextWriter will only let you add the attribute after calling WriteStartElement(). I have extended the previous example to add the attrubite. The code self contains comments which will explain you the steps to create the XML file.

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XSLT 





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML