[Solved] C# how xmlreader read attributes element value


With huge xml files I use combination of XML Reader and XML Linq

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;



namespace ConsoleApp2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";   
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            while(!reader.EOF)
            {
                if (reader.Name != "Subject")
                {
                    reader.ReadToFollowing("Subject");
                }
                if(!reader.EOF)
                {
                    XElement subject = (XElement)XElement.ReadFrom(reader);
                    string _class = (string)subject.Attribute("class");
                    string grade = (string)subject.Attribute("grade");
                    string id = (string)subject.Attribute("id");
                    string conDN = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "conDN").First();
                    string code = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "Code").First();
                    string product = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "product").First();
                    string serial = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "serial").First();

                    Console.WriteLine("class = {0}, grade = {1}, id = {2}, conDN = {3}, code = {4}, product = {5}, serial = {6}", 
                        _class, grade, id, conDN, code, product, serial);
                }
            }
            Console.ReadLine();
 
        }
    }
}

2

solved C# how xmlreader read attributes element value