We help IT Professionals succeed at work.

XMLReader  ReadSubTree always returns None

Janice Hobden
on
456 Views
Last Modified: 2017-06-09
My XML looks like this:
<WHOLE_PAGE>
<CONSULTATION_PAGE SCREEN_NAME="Which Version">
<MESSAGE/>
<LEGEND/>
<QUESTION_ANSWER_GROUP QA_GROUP_ID="1">
<PROMPT/>
<QUESTION_ANSWER FIELD_NAME="which_version" OBJECT_NAME="Which_Version">
<QUESTION/>
<ANSWER STRING_LENGTH="" DISPLAY_LENGTH="5" DATA_TYPE="" ENTRY_MODE="Edit">1.00</ANSWER>
</QUESTION_ANSWER>
</QUESTION_ANSWER_GROUP>
</CONSULTATION_PAGE>

<CONSULTATION_PAGE SCREEN_NAME="Customer Info">
<MESSAGE/>
<LEGEND/>
<QUESTION_ANSWER_GROUP QA_GROUP_ID="1">
<PROMPT/>
<QUESTION_ANSWER FIELD_NAME="name" OBJECT_NAME="name">
<QUESTION/>
<ANSWER STRING_LENGTH="" DISPLAY_LENGTH="30" DATA_TYPE="" ENTRY_MODE="Edit">Fred</ANSWER>
</QUESTION_ANSWER>
</QUESTION_ANSWER_GROUP>

<QUESTION_ANSWER_GROUP QA_GROUP_ID="2">
<PROMPT/>
<QUESTION_ANSWER FIELD_NAME="surname" OBJECT_NAME="surname">
<QUESTION/>
<ANSWER STRING_LENGTH="" DISPLAY_LENGTH="30" DATA_TYPE="" ENTRY_MODE="Edit">Flintstone</ANSWER>
</QUESTION_ANSWER>
</QUESTION_ANSWER_GROUP>
</CONSULTATION_PAGE>

</WHOLE_PAGE>


What I'm trying to do is on reaching <CONSULTATION_PAGE> I want to create a new reader to read all the children of this element but it always returns NONE.  

This is my first time using XMLReader so its new to me... Can anyone point out the error in my code. Thanks

 static void Main(string[] args)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            XmlReader xmlReader = XmlReader.Create("C:/temp/Migration/XMLTEST_001.xml");
            while (xmlReader.Read())
            {              
                if (xmlReader.IsStartElement("CONSULTATION_PAGE"))                        
                {                                            
                    XmlReader pageNodes = xmlReader.ReadSubtree();                                                                        
                }
          }
     }

Thanks
Comment
Watch Question

it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Here is one way you could read the data.  Note this is definately not the best way but it should give you a general idea:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace EE_Q29009171
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = XElement.Load("XMLFile1.xml");
            var pages = (from page in document.Descendants("CONSULTATION_PAGE")
                         select new
                         {
                             ScreenName = page.Attribute("SCREEN_NAME").Value,
                             Message = page.Element("MESSAGE").Value,
                             Legend = page.Element("LEGEND").Value,
                             QAGroup = new List<dynamic>(from qaGroup in page.Descendants("QUESTION_ANSWER_GROUP")
                                                         select new
                                                         {
                                                             ID = qaGroup.Attribute("QA_GROUP_ID").Value,
                                                             Prompt = qaGroup.Element("PROMPT").Value,
                                                             QA = new List<dynamic>(from qa in qaGroup.Descendants("QUESTION_ANSWER")
                                                                                    select new
                                                                                    {
                                                                                        FieldName = qa.Attribute("FIELD_NAME").Value,
                                                                                        ObjectName = qa.Attribute("OBJECT_NAME").Value,
                                                                                        Question = qa.Element("QUESTION").Value,
                                                                                        Answer = new
                                                                                        {
                                                                                            StringLength = qa.Element("ANSWER").Attribute("STRING_LENGTH").Value,
                                                                                            DisplayLength = qa.Element("ANSWER").Attribute("DISPLAY_LENGTH").Value,
                                                                                            DateType = qa.Element("ANSWER").Attribute("DATA_TYPE").Value,
                                                                                            EntryMode = qa.Element("ANSWER").Attribute("ENTRY_MODE").Value,
                                                                                            Value = qa.Element("ANSWER").Value
                                                                                        }
                                                                                    })
                                                         })
                         });

            foreach (var page in pages)
            {
                Console.WriteLine(page);
                foreach (var qaGroup in page.QAGroup)
                {
                    Console.WriteLine(qaGroup);
                    foreach (var qa in qaGroup.QA)
                    {
                        Console.WriteLine(qa);
                    }
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Open in new window


Produces the following output -Capture.PNG
-saige-

Author

Commented:
Hi, thanks for your feedback.  I actually already have another solution that uses xml document and does read all the elements that I need and processes them (I am migrating xml into a sql table so need very specific parts of the xml), but it's just too slow using standard xml parsing.  I read that XMLReader is much quicker which is why I am now trying this route.  At present it looks like my current method will run for 12 hours as I have so many thousands of xml files to migrate, so performance is the key issue. I'm hoping if I resolve this to cut this down to maybe just an hour or two. But I appreciate your time and speedy response.
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thank you so much, I am going to try that in my code.  I really do appreciate your assistance.
CERTIFIED EXPERT

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: it_saige (https:#a42049257)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer