none
Error in XML document RRS feed

  • Question

  • Error in XML document (97, 124). - in System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) in System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)

    Hi, I have the following XML document:

    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <r k="KEY1" v="VALUE1" />
      <r k="KEY2" v="VALUE2" />
      ...
    </items>


    I tried to deserialize it doing the following operation:

    private Dictionary<string, string> deserializeDictionary(string path)
    {
       Dictionary<string, string> deserializedDictionary = new Dictionary<string, string>();
       StreamReader sr = File.OpenText(path);
       XmlSerializer serializer = new XmlSerializer(typeof(r[]), new XmlRootAttribute() { ElementName = "items" });
       deserializedDictionary = ((r[])serializer.Deserialize(sr)).ToDictionary(i => i.key, i => i.value);
       sr.Close();
       return deserializedDictionary;
    }


    where r is:

    public class r
    {
      [XmlAttribute(AttributeName = "k")]
      public string key;
      [XmlAttribute(AttributeName = "v")]
      public string value;
    }

    but at 

    deserializedDictionary = ((r[])serializer.Deserialize(sr)).ToDictionary(i => i.key, i => i.value);

    line, an exception occurs:

    Error in XML document (97, 124). - in System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) in System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)

    Why I get this error?

    Thank You.

    EDIT:

    Inner Exception:

    The character '', with hexadecimal value 0x01, is not valid. Line 97, position 124.


    Thursday, November 7, 2019 10:25 AM

Answers

  • For someone who is interested, I solved my problem in the following way:

    private static string cleanInvalidXmlChars(string text)
    {
      const string invalidCharacters = @"&#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|[19][0-9A-F]|7F|8[0-46-9A-F]|0?[1-8BCEF]);";           
      string newString = string.Empty;
      Regex regex = new Regex(invalidCharacters, RegexOptions.IgnoreCase);
      if (regex.IsMatch(text))
         newString = regex.Replace(text, "");
      else
         newString = text;
         return newString.ToString();
    }
    and I called this method on a string retrieved by
    string str = File.ReadAllText("path\of\my\file")



    Thursday, November 7, 2019 1:12 PM