XMLReader ReadSubTree always returns None

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
Janice HobdenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-
0
Janice HobdenAuthor 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.
0
it_saigeDeveloperCommented:
In that case, here is what I come up with using the XmlReader:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace EE_Q29009171
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var reader = XmlReader.Create("XMLFile1.xml", new XmlReaderSettings() { IgnoreWhitespace = true }))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement("CONSULTATION_PAGE"))
                    {
                        using (var sub = reader.ReadSubtree())
                        {
                            while (sub.Read())
                            {
                                Console.WriteLine("{0}: {1}", sub.Name, sub.Value);
                                if (sub.HasAttributes)
                                {
                                    Console.WriteLine("Attributes:");
                                    while (sub.MoveToNextAttribute())
                                    {
                                        Console.WriteLine("{0}: {1}", sub.Name, sub.Value);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Janice HobdenAuthor Commented:
Thank you so much, I am going to try that in my code.  I really do appreciate your assistance.
0
frankhelkCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.