.NET and XML
.NET and XML Contents
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
Partners
Dream.In.Code dotNet Slackers dotNet Spider Your HTML Source VisualBuilder.com DevGuru Planet Source Code ZVON.ORG Web Design ASPAlliance XML Pitstop Scripts
The Spot 4 SAP Bitshop Web Hosting