Get the the number of attributes using XmlReader in CSharp

Description

The following code shows how to get the the number of attributes using XmlReader.

Example


using System;/*from   ww  w  .  ja  v  a 2  s  . c  om*/
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 v='a'>
                      <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();
            }
        }
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style