Source Error:

The input string had the wrong format.

Description: An unhandled exception occurred during the execution of the current web request. Examine the stack trace for more information about this error and the code snippet that caused it.

Exception Details: System.FormatException: The input string had an invalid format.

Строка 192: using (StreamReader sr2 = new StreamReader(stream)) Строка 193: { Строка 194: adsList = (List<Ad>)serializer.Deserialize(sr2); Строка 195: sr2.Close(); Строка 196: } 

Full listing:

 [Serializable()] public class Ad { [XmlElement("Id")] public int Id { get; set; } // это поле встречается не во всех нодах xml файла [XmlElement("Country")] public string Country { get; set; } } public static void AdGet(string FileUrl, string filesrc) { XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "Ads"; xRoot.IsNullable = true; XmlSerializer serializer = new XmlSerializer(typeof(List<Ad>),xRoot); String strResult; WebResponse objResponse; var adsList = new List<Ad>(); WebRequest objRequest = HttpWebRequest.Create(FileUrl); objResponse = objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { strResult = sr.ReadToEnd(); strResult = strResult.Replace(" formatVersion=\"3\"", ""); sr.Close(); } MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(strResult); writer.Flush(); stream.Position = 0; using (StreamReader sr2 = new StreamReader(stream)) { adsList = (List<Ad>)serializer.Deserialize(sr2); sr2.Close(); } } 
  • 2
    Give a more complete listing. And what a mistake? - eastwing
  • It is clear that the class definition does not match the xml schema. Show the xml you are trying to deserialize. Then you can give advice on what to fix. - Alexander Petrov

1 answer 1

The serializer needs to specify with what type of object it needs to work.

 var serializer = new XmlSerializer(typeof(MyObject)); 

if it did not help, then you mean the class is wrong: the discrepancy in properties, there is no attribute [Serializable] , the class has no standard constructor without parameters.

  • The [Serializable] attribute of the XmlSerializer class is indifferent. - Alexander Petrov