Error in XML structure. How to skip node and continue using XmlDocument

P

Peter

Hello,

I am having trouble processing an XML file that contains
an error on one of the nodes. How can I instruct the .NET
framework to skip the node and continue when it comes
across a node that contains an error? Please advise.


This is the error:
Hexadecimal value 0x1C is an invalid character
Line 5810, position 83


// Here is my code
XmlDocument doc = new XmlDocument();
FileStream fs = new FileStream(strXmlFileName,
FileMode.Open);
doc.Load(fs);

// Get the node list -- line below is where the error
occurs
XmlNodeList catalog = doc.GetElementsByTagName("product");


P.S - The XML data file is coming from a third party so
we cannot fix the error in it, we just have to deal with
it.
 
J

Jon Skeet [C# MVP]

Peter said:
I am having trouble processing an XML file that contains
an error on one of the nodes. How can I instruct the .NET
framework to skip the node and continue when it comes
across a node that contains an error? Please advise.

I would expect that to be really pretty difficult, unfortunately. What
I suggest you do is work out a way to correct the document on its way
in - write a filtering stream which just gets rid of the invalid bytes
(or use a reader and get rid of the invalid characters). XML parsers
are typically not designed to do error correction, only error
detection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top