Search code examples
c#xmlreader

C# how xmlreader read attributes element value


I have tried many way to read the value A180 in <p name="conDN">A180</p>, but fail.

I would like to read all values for the below name:

  • class, grade, id
  • conDN, Code, product, serial

Attribute Name = name;
Attribute value = conDN;
i really no idea how to call A180
** I am newbie in C#.**

May the expert guide me on how to read it.

Thank you very much.

Below is the XML content:

<Subject class="High" grade="E21" id="78038751482">
  <p name="conDN">A180</p>
  <p name="Code">AA98073512</p>
  <p name="product">ACN5677</p>
  <p name="serial">AEXB18201</p>
</Subject>
<Subject class="Low" grade="E21" id="98721124">
  <p name="conDN">B900</p>
  <p name="Code">BA723512</p>
  <p name="product">BG5677</p>
  <p name="serial">BCB18148</p>
</Subject>
<Subject class="Middle" grade="E2022" id="52358523205">
  <p name="conDN">C800</p>
  <p name="Code">CA47351299</p>
  <p name="product">CA5677</p>
  <p name="serial">CTB18201</p>
</Subject>

Please find below is my code:

string attName = null;
string attValue = null;
do
{

    reader.MoveToFirstAttribute();

    attName = "class";
    if (reader.MoveToAttribute(attName))
        attValue = reader.ReadContentAsString();
    tbox01.AppendText($"{attName} : {attValue} \n");

    attName = "grade";
    if (reader.MoveToAttribute(attName))
        attValue = reader.ReadContentAsString();
    tbox01.AppendText($"{attName} : {attValue} \n");

    attName = "id";
    if (reader.MoveToAttribute(attName))
        attValue = reader.ReadContentAsString();
    tbox01.AppendText($"{attName} : {attValue} \n");


    //------------ Read Element --------------

    reader.ReadToFollowing("p");
    attName = "name";
    if (reader.MoveToAttribute(attName))
        attValue = reader.ReadContentAsString();
    tbox01.AppendText($"{attName} : {attValue} \n");


    tbox01.AppendText("-------------------------");
        tbox01.AppendText(Environment.NewLine);


} while (reader.ReadToFollowing("Subject") );

Below is what i get with my code:

class : High 
grade : E21 
id : 78038751482 
name : conDN 
-------------------------
class : Low 
grade : E21 
id : 98721124 
name : conDN 
-------------------------
class : Middle 
grade : E2022 
id : 52358523205 
name : conDN 
-------------------------
------------ END -------------

Solution

  • 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").FirstOrDefault();
                        string code = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "Code").FirstOrDefault();
                        string product = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "product").FirstOrDefault());
                        string serial = (string)subject.Elements().Where(x => (string)x.Attribute("name") == "serial").FirstOrDefault();
    
                        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();
     
            }
        }
    }