Solved

XML Exception: Invalid Character(s)

Posted on 2009-05-12
4
1,416 Views
Last Modified: 2013-11-11
I am working on a small project that is receiving XML data in string form from a long running application. I am trying to load this string data into an XDocument (System.Xml.Linq.XDocument), and then from there do some XML Magic and create an xlsx file for a report on the data.

On occasion, I receive the data that has invalid XML characters, and when trying to parse the string into an XDocument, I get this error.

[System.Xml.XmlException]
Message: '?', hexadecimal value 0x1C, is an invalid character.

Since I have no control over the remote application, you could expect ANY kind of character.

I am well aware that XML has a way where you can put characters in it such as &#x1C or something like that.

If at all possible I would SERIOUSLY like to keep ALL the data. If not, than let it be.


---

I have thought about editing the response string programatically, then going back and trying to re-parse should an exception be thrown, but I have tried a few methods and none of them seem successful.

Thank you for your thought.
TextReader  tr;
XDocument  doc;
string           response; //XML string received from server.
 
...
 
tr = new StringReader (response);
 
try
{
     doc = XDocument.Load(tr);
}
catch (XmlException e)
{
    //handle here?
}

Open in new window

0
Comment
Question by:Meiscooldude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
ViceroyFizzlebottom earned 500 total points
ID: 24367911
Here is something I did a while ago when faced with the same issue. Basically, read in the data as plain text, manipulate it how you want to get it massaged, then load that into your XML doc.
                using (StreamReader reader = _xmlCatalogFile.OpenText())
                {
                    string strRawData = reader.ReadToEnd();
                    reader.Close();
 
                    // Replace malformed data
                    Regex badAmpersand = new Regex("&(?![a-zA-Z]{2,6};|#[0-9]{2,4};)");
                    const string goodAmpersand = "&";
                    strRawData = badAmpersand.Replace(strRawData, goodAmpersand);
 
                    _xmlDocument.LoadXml(strRawData);
                }

Open in new window

0
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24367916
The regular expression above was simply to format '&' but the general idea can be used for anything.
0
 

Author Comment

by:Meiscooldude
ID: 24368086
Thank you for the hasty reply,

From what i can see, that will only replace an ampersand if it is in front of a hex char or something like 'gt;'

I am looking for a way to replace ALL invalid characters, such as 'G' with their corresponding &#hexvalue or simply removing it all together. (preferably keeping it)
0
 

Author Closing Comment

by:Meiscooldude
ID: 31580658
I used a method like this, thank you vm
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question