<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Whaly&#039;s World &#187; xml</title>
	<atom:link href="http://baleinoid.com/whaly/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://baleinoid.com/whaly</link>
	<description>tail -f /var/log/whaly</description>
	<lastBuildDate>Fri, 06 Apr 2012 21:28:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Xml deserialization error with invalid character</title>
		<link>http://baleinoid.com/whaly/2011/08/xml-deserialization-invalid-character/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=xml-deserialization-invalid-character</link>
		<comments>http://baleinoid.com/whaly/2011/08/xml-deserialization-invalid-character/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 19:49:39 +0000</pubDate>
		<dc:creator>whaly</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Microsoft .NET]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://baleinoid.com/whaly/?p=193</guid>
		<description><![CDATA[Recently I had a strange error, I serialized a complex object and during the deserialization process I got : "Error : System.Xml.XmlException: '.', hexadecimal value 0x00, is an invalid character. Line X, position Y." It appears that we had a "\0" inside a string, something like "Hello\0World" ! and during a classic serialization, the character [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had a strange error, I serialized a complex object and during the deserialization process I got :<br />
"Error : System.Xml.XmlException: '.', hexadecimal value 0x00, is an invalid character. Line X, position Y."<br />
It appears that we had a "\0" inside a string, something like "Hello\0World" ! and during a classic serialization, the character was encoded in "&amp;#x0;"</p>
<p>With the help of Google I have found <a href="http://tjoe.wordpress.com/2007/08/23/xml-serialization-sorrows/" title="XML Serialization Sorrows">this post</a> (2007) with a way to have an happy deserialization <img src='http://baleinoid.com/whaly/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>You can use XmlTextReader instead of XmlReader, but with more research I have found that you can still use XmlReader with XmlReaderSettings and CheckCharacters set to false.</p>
<p>Here is an example :</p>
<pre class="brush: csharp; title: ; notranslate">
    public class MyObject
    {
        public string MyString { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            XmlSerializerFactory fact = new XmlSerializerFactory();
            XmlSerializer ser = fact.CreateSerializer(typeof(MyObject));

            MyObject obj0 = new MyObject();
            obj0.MyString = &quot;Hello&#92;&#48;World&quot;;

            // Serialize the object
            StringWriter sw = new StringWriter();
            ser.Serialize(sw, obj0);
            string xml = sw.ToString();
            // We can check that in the xml a &#92;&#48; is transformed in &amp;#x0;
            Console.WriteLine(xml);

            // Classic use of XmlReader.Create
            StringReader sr1 = new StringReader(xml);
            XmlReader xr1 = XmlTextReader.Create(sr1); // xr1's type is XmlTextReaderImpl
            try
            {
                MyObject obj1 = (MyObject)ser.Deserialize(xr1);
                Console.WriteLine(&quot;XmlReader [CheckCharacters({0})] : Success : {1}&quot;, xr1.Settings.CheckCharacters, obj1.MyString);
                Console.WriteLine(obj1.MyString);
            }
            catch (Exception e)
            {
                Console.WriteLine(&quot;XmlReader [CheckCharacters({0})] : Error : {1}&quot;, xr1.Settings.CheckCharacters, e.InnerException);
            }

            // Using an XmlTextReader
            StringReader sr2 = new StringReader(xml);
            XmlTextReader xr2 = new XmlTextReader(sr2);
            // xr2.Settings is null
            MyObject obj2 = (MyObject)ser.Deserialize(xr2);
            Console.WriteLine(&quot;XmlTextReader : Success : {0}&quot;, obj2.MyString);

            // Using XmlReader with the good XmlReaderSettings
            StringReader sr3 = new StringReader(xml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.CheckCharacters = false; // default value is true;
            XmlReader xr3 = XmlTextReader.Create(sr3, settings); // xr3.Settings.CheckCharacters is a read only and xr3's type is XmlTextReaderImpl
            MyObject obj3 = (MyObject)ser.Deserialize(xr3);
            Console.WriteLine(&quot;XmlReader [CheckCharacters({0})] : Success : {1}&quot;, xr3.Settings.CheckCharacters, obj3.MyString);
        }
    }
</pre>
 <p><a href="http://baleinoid.com/whaly/wordpress/?flattrss_redirect&amp;id=193&amp;md5=6375279dccfd928b26b4191b0e573915" title="Flattr" target="_blank"><img src="http://baleinoid.com/whaly/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://baleinoid.com/whaly/2011/08/xml-deserialization-invalid-character/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=whaly&amp;popout=1&amp;url=http%3A%2F%2Fbaleinoid.com%2Fwhaly%2F2011%2F08%2Fxml-deserialization-invalid-character%2F&amp;language=en_GB&amp;category=text&amp;title=Xml+deserialization+error+with+invalid+character&amp;description=Recently+I+had+a+strange+error%2C+I+serialized+a+complex+object+and+during+the+deserialization+process+I+got+%3A+%22Error+%3A+System.Xml.XmlException%3A+%27.%27%2C+hexadecimal+value+0x00%2C+is+an+invalid+character....&amp;tags=c%23%2Cxml%2Cblog" type="text/html" />
	</item>
		<item>
		<title>XmlWriter and UTF-8 encoding without signature</title>
		<link>http://baleinoid.com/whaly/2009/07/xmlwriter-and-utf-8-encoding-without-signature/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=xmlwriter-and-utf-8-encoding-without-signature</link>
		<comments>http://baleinoid.com/whaly/2009/07/xmlwriter-and-utf-8-encoding-without-signature/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 19:41:13 +0000</pubDate>
		<dc:creator>whaly</dc:creator>
				<category><![CDATA[Microsoft .NET]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://baleinoid.com/whaly/?p=3</guid>
		<description><![CDATA[I used this code to serialize some objects in Xml : But the output contains an UTF header, the Byte Order Mark (BOM). The use of the header/signature is usually for xml file, if you want to use the ouput in an HttpResponse, you don't need the signature. (some parser can cause a parsing error [...]]]></description>
			<content:encoded><![CDATA[<p>I used this code to serialize some objects in Xml :</p>
<pre class="brush: csharp; light: true; title: ; notranslate">XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8);</pre>
<p>But the output contains an UTF header, the Byte Order Mark (BOM). The use of the header/signature is usually for xml file, if you want to use the ouput in an HttpResponse, you don't need the signature. (some parser can cause a parsing error in java, like org.xml.sax.SAXException).</p>
<p>Here is the change to remove the BOM :</p>
<pre class="brush: csharp; light: true; title: ; notranslate">XmlWriter writer = new XmlTextWriter(stream, new UTF8Encoding(false));</pre>
 <p><a href="http://baleinoid.com/whaly/wordpress/?flattrss_redirect&amp;id=3&amp;md5=ef99945c85c9b851c21f2490f8525ae3" title="Flattr" target="_blank"><img src="http://baleinoid.com/whaly/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://baleinoid.com/whaly/2009/07/xmlwriter-and-utf-8-encoding-without-signature/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=whaly&amp;popout=1&amp;url=http%3A%2F%2Fbaleinoid.com%2Fwhaly%2F2009%2F07%2Fxmlwriter-and-utf-8-encoding-without-signature%2F&amp;language=en_GB&amp;category=text&amp;title=XmlWriter+and+UTF-8+encoding+without+signature&amp;description=I+used+this+code+to+serialize+some+objects+in+Xml+%3A+But+the+output+contains+an+UTF+header%2C+the+Byte+Order+Mark+%28BOM%29.+The+use+of+the+header%2Fsignature+is+usually...&amp;tags=c%23%2Cencoding%2Cutf8%2Cxml%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: baleinoid.com @ 2012-05-22 07:26:07 -->
