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 avoid to read the XML declaration with XmlTextReader?
How to create a XML file with XmlTextWriter?
Is it possible to add attributes with XmlTextWriter?
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?


 
 

<< XSLT 


By Sonu Kapoor
First Posted 02/10/2005
Times viewed 2975

How to check if the XML elements contains any attributes with XmlTextReader


Summary How to check if the XML elements contains any attributes with XmlTextReader and HasAttributes?

Sometimes it can be important to read the attribute values of an xml element. This small example will show you how you can verify if the element contains an attribute and how you can obtain the attribute value. In the below XML file, you will see that the first MainCategory element does not contain any ID attribute, but all others do.

The code loads the xml file into the XmlTextReader. The first while loop just reads all nodes one by one. In that loop I am using the HasAttributes property which returns true if any attribute is found. If we have found an attribute then we start a second while loop which iterates through all attributes with the function MoveToNextAttribute(). Finally we print out the result.

Because the first MainCategory element does not have any attribute we got the following output.

MainCategory ID: 1 MainCategory ID: 2 MainCategory ID: 3 MainCategory ID: 4

Additional information

Further additional information


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

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