Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.Xml.XmlException Invalid Character

Status
Not open for further replies.

CodingIsFun

Programmer
Apr 9, 2004
134
US
Hi all experts

I am parsing through a 5 gig xml file, copying to another file as I go. I need to find a particular part of the xml and change a value for the new file.

Here is the error I get.

System.Xml.XmlException was unhandled
Message="Invalid character in the given encoding. Line 283, position 39709."
Source="System.Xml"
LineNumber=283
LinePosition=39709....

Unfortunately I can not find the foreign character.
Is there anyway to change the encoding of the XmlTextReader.

here is a snippet of my code, it fails at textReader.ReadOuterXml();

XmlTextReader textReader = new XmlTextReader(file_name_in);
textReader.Read();
while (!textReader.EOF)
{
XmlDocument doc = new XmlDocument();
/*try
{*/
string val = textReader.ReadOuterXml();
if (!val.Equals(""))//avoid empty reads
{
doc.LoadXml(val);
XmlNode inv = doc.SelectSingleNode(xpath);
if (inv != null)
{
string acid = inv.Attributes.GetNamedItem("acId").Value;
if (autopay_accounts.IndexOf(acid) != -1)
{
inv.Attributes.GetNamedItem("type").Value = "P";
log_writer.WriteLine("Changed account_id: " + acid + " to type=\"P\" ");
}
}
write.Write(doc.InnerXml);
//update the invoice element here.
}
else
{
write.Write(val);
}
/*
}
catch (Exception error)
{//ReadElementContentAsString()
log_writer.WriteLine("Error reading: " + textReader.);
}
*/
}

I have tested this on smaller files and it works fine. This gets through about 1.5 gigs before the error.

I have parsed line by line, which are .25 gigs per line to try and identify the un-recognizable character. I still can't find it.

Any help would be greatly appreciated.

thanks in advance..
 
Try a while loop surrounding a ReadChars() call.
That will probably be finer-grained and let you find the offending character.

But it's been my experience that you have to go back to the person/system that produced the file and ask them why they sent you an invalid XML document. It's probably because the built it using string concatenation, and not by doing it like they should with XML-aware methods/objects.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Finding the character that is causing the failure doesn't help me actually. Is there a way to change the encoding for the XmlTextReader so it doesn't error?

thanks again for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top