0

I have the following code I'm using to parse XML strings that contain collections of objects. I want to capture the XML for any given object as a string if I can't parse it. I want store it and analyze it if it won't parse correctly. I would prefer not to make a radical change, but I can't figure out how to grab that part of the XML which is invalid and capture it. xmlReader.ReadOuterXml() throw an exception when it can't parse. Thanks in advance.

// we want to read each canonical item in the report from oracle separately
            while (xmlReader.ReadToFollowing(ROOT_ELEMENT))
            {
                string itemXml = String.Empty;

                try
                {
                    // this gives us the whole segment of xml including the root element tag
                    itemXml = xmlReader.ReadOuterXml();
                    xmlReader.
                    T processedItem = default;

                    processedItem = reportMapper.Mapper(itemXml);
                    successfulItems.Add(new ProcessingResult<T>()
                    {
                        ProcessedItem = processedItem
                    });
                }
Danny Ellis Jr.
  • 1,674
  • 2
  • 23
  • 38
  • You might try reading from a MemoryStream (or a custom Stream wrapper) and examine the position in case of an exception. – David Browne - Microsoft Apr 24 '20 at 13:59
  • Can you create a schema and validate against it? The Validation will return the position of the first error it detects. – Tony Hopkinson Apr 24 '20 at 14:07
  • @DavidBrowne-Microsoft, I'm looking at similar ideas, but they are all quite complex. – Danny Ellis Jr. Apr 24 '20 at 14:10
  • @TonyHopkinson, I'll explore this option, interesting idea. – Danny Ellis Jr. Apr 24 '20 at 14:10
  • I did it, the errors though are often opaque. The more specific the schema, the more you get out of it. – Tony Hopkinson Apr 24 '20 at 14:12
  • Does this answer your question? [How to parse invalid (bad / not well-formed) XML?](https://stackoverflow.com/questions/44765194/how-to-parse-invalid-bad-not-well-formed-xml) – Progman Apr 24 '20 at 16:42
  • @DannyEllisJr. Why do you get invalid XML in the first place? Where do you get it from? – Progman Apr 24 '20 at 16:43
  • 1
    You need to be clear whether you are talking about ill-formed XML (which is not really XML at all), or invalid XML (XML that's well-formed but doesn't match some schema). – Michael Kay Apr 24 '20 at 17:00
  • @Progman It comes from a 3rd party. – Danny Ellis Jr. Apr 24 '20 at 19:36
  • @MichaelKay, that's a good point. Malformed XML isn't likely. The error that brought this up was a number too large for a long being in the XML. – Danny Ellis Jr. Apr 24 '20 at 19:38
  • OK, so it looks like a schema validation error. It could be violating limits actually defined in the schema, or it could be violating "default" limits defined by the schema validator (you seem to be using the Microsoft .NET schema validator). Not being an expert on that product or its API, I can't really help you any further, sorry! – Michael Kay Apr 24 '20 at 22:57

0 Answers0