Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML Exception: Invalid Character(s)

Posted on 2009-05-12
4
Medium Priority
?
1,460 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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