• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 683
  • Last Modified:

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;

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

                while (reader.ReadToFollowing("product"))

                return recordCount;
            catch (IOException ex)
                MessageBox.Show(ex.ToString(), "There was an error in getProductCount");
                return 0;
1 Solution
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 ...
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.

nvbobAuthor Commented:
Nice clean approach.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now