Ajay Patel

Ajay Patel

  • NA
  • 147
  • 255.2k

XML Serialization issue

Jun 25 2013 6:57 AM
Hello,

When I serialize the XML with tag name including colon, then I get following result after serialization

<?xml version="1.0" encoding="utf-16"?>
<Test_x003A_11 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <str1>A1:A#</str1>
  <str2>A2</str2>
  <str3>A3</str3>
</Test_x003A_11>


I have following code to generate this XML.

 test tc = new test() { str1 = "A1:A#", str2 = "A2", str3 = "A3" };
var serializer = new XmlSerializer(typeof(test));
var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream, System.Text.Encoding.Unicode);
serializer.Serialize(streamWriter,tc);           
Byte[] utf8EncodedXml = memoryStream.ToArray();
var stream = new StreamReader(new MemoryStream(utf8EncodedXml));
var cs = stream.ReadToEnd();

[XmlRoot("Test:11")]
    public class test
    {
        public string str1 { get; set; }
        public string str2 { get; set; }
        public string str3 { get; set; }
    }

Answers (7)