Use ReadToFollow when character case is not known

I am reading through an XML files using ReadToFollow.  Works great.  The challenge is that the XML file can come from many different sources.  Yes they all are supposed to follow a standard but reality is that not everyone follows the standards.  In any case the element names are not all cased the same.  For example they can be <product>, <Product> or <PRODUCT>.  Is there a way to use ReadToFollow but get around the case sensitivity?

Easy example of code being used –

            Int64 recordCount = 0;

            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.DtdProcessing = DtdProcessing.Ignore;
            readerSettings.ConformanceLevel = ConformanceLevel.Document;
            readerSettings.IgnoreWhitespace = true;
            readerSettings.IgnoreComments = true;
            readerSettings.XmlResolver = null;

            try
            {
                XmlReader reader = XmlReader.Create(inputFileName, readerSettings);

                while (reader.ReadToFollowing("product"))
                {
                    recordCount++;
                }

                return recordCount;
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.ToString(), "There was an error in getProductCount");
                return 0;
            }
nvbobAsked:
Who is Participating?
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
the underlying issue is simple: XML is case sensitive.

so, the correct solution is: get all your input source HAVE to follow the same rules.

alternative is to pre-parse the xml to "uppercase" all the XML node names and attributes, OR not to use ReadToFollowing ...
0
sarabandeCommented:
if we assume that the cases do not change within one file and that "product", "Product", and "PRODUCT" are the only choices, you may check with a little function, that takes a copy of the newly created reader, which of the 3 spellings was used in the file and use that:

bool YourClass::checkSpellings(XmlReader^ reader, String^ name)
{
     return (reader.ReadToFollowing(name));
}

...
if (checkSpellings(XmlReader.Create(reader, readerSettings), "product")
{
       // ok
       nameToUse = "product";

Open in new window

you would call it for all possible spellings until it returns true. then, you would start the while loop with the "found" name.

Sara
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
nvbobAuthor Commented:
Nice clean approach.
Thanks
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
Programming Languages-Other

From novice to tech pro — start learning today.