Link to home
Start Free TrialLog in
Avatar of nvbob
nvbob

asked on

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;
            }
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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 ...
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nvbob
nvbob

ASKER

Nice clean approach.
Thanks