Deserialization error in XML document (1,1)

I have an XML file that I am deserializing, the funny part is an XML file that has been serialized using the following code:

enter code here var serializer = new XmlSerializer(typeof(CommonMessage)); var writer = new StreamWriter("OutPut.txt"); serializer.Serialize(writer, commonMessage); writer.Close(); 

And I'm trying to deserialize it again to check if the result matches the input signal. anyway, here is my code for deserializing:

 var serializer = new XmlSerializer(typeof(CommonMessage)); var reader = new StringReader(InputFileName); CommonMessage commonMessage = (CommonMessage)serializer.Deserialize(reader); 
+8
c # xml-deserialization
source share
3 answers

Replace StringReader with StreamReader and it will work fine. StringReader reads the value from the string (which is the file name in your case).

+23
source share

I just had the same error message, but with a different source of errors. If someone has the same problem as mine. I chopped off the first char my xml line by splitting the lines. And the xml line is damaged:

 "?xml version="1.0" encoding="utf-16"?> ..." // my error "<?xml version="1.0" encoding="utf-16"?> ..." // correct 

(1,1) means that first the first char of the first line is incorrect, and the line cannot be deserialized.

0
source share

include the XmlRoot element tag in your CommonMessage class using xmlroot, for example: [XmlRoot ("UIIVerificationResponse")]

0
source share

Source: https://habr.com/ru/post/651215/


All Articles