I need to get a serialized XML representation of an object as a string. I am using XmlSerializer and memoryStream for this.
XmlSerializer serializer = new XmlSerializer(typeof(MyClass)); using (MemoryStream stream = new MemoryStream()) { using (XmlTextWriter writer = new XmlTextWriter(stream,Encoding.UTF8)) { serializer.Serialize(writer, myClass); string xml = Encoding.UTF8.GetString(stream.ToArray());
Now just look at the xml.substring lines for a moment. What I find is that (even I thought I was pointing the encoding to XmlTextWriter and to GetString (and I use memoryStream.ToArray (), so I only work with data in the stream buffer) .. As a result, in the line xml added an invalid character other than xml. In my case, โ?โ is at the beginning of the line. That's why I am a substring for '<' and '>' to ensure that I "I only get good things.
Strange, looking at this line in the debugger (Text Visualizer), I do not see this ??. Only when I paste what's in the visualizer into a notebook or the like.
So, while the above code (substring, etc.) does the job, what actually happens here? Is some unsigned byte thing included and not represented in the text renderer?
source share