Mega Code Archive

 
Categories / C# / XML
 

XmlReader AttributeCount Property returns the number of attributes

using System; using System.Linq; using System.Xml; using System.IO; using System.Xml.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {     public static void Main()     {         String xmlString = @"<?xml version='1.0'?>                     <!-- This is a sample XML document -->                     <Items>                       <Item>test with a child element <more/> stuff</Item>                     </Items>";         using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))         {             if (reader.HasAttributes)             {                 Console.WriteLine("Attributes of <" + reader.Name + ">");                 for (int i = 0; i < reader.AttributeCount; i++)                 {                     Console.WriteLine("  {0}", reader[i]);                 }                 reader.MoveToElement();             }         }     } }