I don't know  if people are aware of the below exception. I wanted to throw some light on the issue, and help stranded people solve this issue.
Exception information:
Exception type: ArgumentException
Exception message: '', hexadecimal value 0x01, is an invalid character.

I support a web service which is heavily being used in the company, users who were consuming the web service where reporting problems that it was throwing some exception like above. After lot of investigation I solved the issue with  Bill's blog
Ascii control characters are not allowed in XML, they don't conform to xml specification.  So The .net framework when trying to serialize information with ascii control characters into XML , it throws an exception. These characters should not be allowed.

There are two ways to clean up the data and to remove the characters, you have to search the string , here is how you can do it.

 

/// <summary>
/// Remove illegal XML characters from a string.
/// </summary>
public string SanitizeXmlString(string xml)
{
    if (xml == null)
    {
        throw new ArgumentNullException("xml");
    }

    StringBuilder buffer = new StringBuilder(xml.Length);

    foreach (char c in xml)
    {
        if (IsLegalXmlChar(c))
        {
            buffer.Append(c);
        }
    }

    return buffer.ToString();
}
 
 

source : Bill's blog

 

Here is my version of the code using regex.

 
private string SanitizeXmlString(string xml)
{
 
   if (!string.IsNullOrEmpty(xml))
   {
     char[] hexp = new char[7] 
     { '\x00', '\x08', '\x10', '\x19', 
       '\x0B', '\x0F', '\x7F' };
     string pattern = string.Empty;
     pattern = string.Format("[{0}-{1}{2}-{3}{4}-{5}{6}]", 
     hexp[0], hexp[1], hexp[2], 
     hexp[3], hexp[4], hexp[5], hexp[6]);
     xml = Regex.Replace(xml, pattern, "");
    }
   
    return xml;  
}

 

posted on Wednesday, October 21, 2009 4:42 PM | Filed Under [ .NET Framework ]

Comments

Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by MavisMccall27
on 4/9/2011 2:39 AM
Do you opine that is extra expensive to use forum profiles services? Nonetheless, it will be more expensive if you do your submission stuff yourself!
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 4/11/2011 9:36 PM
I come to writers job service, with the aim to get know about this good topic.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by buy essays
on 5/15/2011 6:37 PM
Don't want to worry just about academic grades further? Just buy custom essays and be happy!
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by thesis help
on 5/16/2011 10:13 AM
Wow, this absolutely cleared some things up. One essay i am having doubt with is a explanation for an "who we are" The teacher says that we will have to gouge deep into ourselves and detect what we really are. But I think it would be betterto use dissertation.
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 5/17/2011 4:24 PM
It is not really easy to make really good custom writing about this good topic! Opt for the superior essay writing service to reach academic benefit or buy essays here!
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by buy essays
on 5/17/2011 4:57 PM
If students want to get know about this post, they will order already written essay or buy an essay with the help of research paper writing service.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by custom essay
on 5/17/2011 11:14 PM
If you try to buy essay, you will not be able to stop doing it. Don't opine that is not OK. Don't bother about that and enjoy your free time.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by thesis
on 5/19/2011 7:27 PM
A trustworthy buy thesis service will be a light in the tunnel, just because it will create the management thesis of high quality.
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 5/23/2011 6:00 AM
We probably will come along your superior idea in time of the essays and just tv essay accomplishing. Thence, thank you a lot for work.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by loans
on 11/16/2011 8:10 PM
Don't have money to buy a building? You not have to worry, because it is real to get the business loans to resolve such problems. So take a sba loan to buy everything you require.
Gravatar
# Houston Home Security
on 1/4/2012 6:35 PM
I am quite excited that I came across this website. I did not find out any other informative site on this subject matter prior to visiting your site. This is one of the best websites I have ever come across. I hope you never stop writing informative articles.
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 1/25/2012 7:44 AM
It goes without saying that each successive visit to this website renders a positive and beneficial effect.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by link buying
on 4/14/2012 7:33 AM
Attract tons of hard traffic and buy text links. It's really wide spread function and that aids to lift business.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by custom papers
on 4/23/2012 9:50 AM
There are many writing organizations where you can pay for essays. Nevertheless, the most reputable of them provide the best and supreme quality!
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 5/2/2012 12:28 PM
I tried to become successful, however, I couldn't manage with my academic essays accomplishing. Nonetheless, all changed when I decided to buy a essay on line! I reached everything I wanted!
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 5/3/2012 6:22 AM
Everybody who is seeking for high quality will surely get it if order custom written essays. Even professionals advice to do in such a way.
Gravatar
# re: Ascii control Characters not XML Serialiazible
on 5/3/2012 9:58 AM
Financial situation in the entire world made writing firms reduce costs for papers. People have a chance to buy college papers online for affordable costs at this moment.
Gravatar
# re: Ascii control Characters not XML Serialiazible
Posted by social bookmark
on 5/10/2012 3:48 PM
I didn't hear earlier just about online social bookmarking. Nonetheless, it supposes to be very effective. There is a great reason to utilize the social bookmarking service , which will help with optimization!
Post Comment
Title *
Name *
Email
Url
Comment *
Please add 3 and 3 and type the answer here: