Reading XML with invalid XML Processing Instruction using System.Xml.XmlReader

177 Views Asked by At

I am trying to read XML from a server I'm connecting to. Unfortunately, I don't have a way of correcting this on their end. I am getting an exception:

Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 3.

I believe this is because they begin the data with <? LLSD/XML ?> and given that the third character is a it throws this exception. My question is, how can I get around this? The rest of the data is valid XML. I have been able to parse this successfully in C++ using expat, tinyxml, etc, but System.Xml.XmlReader fails it. Currently, I am doing this:

        public static OSD DeserializeLLSDXml(Stream xmlStream)
        {
            XmlReaderSettings settings = new XmlReaderSettings
            {
                ValidationType = ValidationType.None,
                CheckCharacters = false,
                IgnoreComments = true,
                IgnoreProcessingInstructions = true,
                DtdProcessing = DtdProcessing.Prohibit
            };
            using (XmlReader xrd = XmlReader.Create(xmlStream))
            {
                return DeserializeLLSDXml(xrd);
            }
        }
    
        public static OSD DeserializeLLSDXml(XmlReader xmlData)
        {
            try
            {
                xmlData.Read();
                SkipWhitespace(xmlData);

                xmlData.Read();
                OSD ret = ParseLLSDXmlElement(xmlData);

                return ret;
            }
            catch (XmlException ex)
            {
                string exs = ex.ToString();
                return new OSD();
            }
        }

Example message I receive:

<? LLSD/XML ?>\n
<llsd>
  <map>
    <key>MINUTES</key>
    <integer>5</integer>
    <key>NAME</key>
    <string>Hippotropolis</string>
  </map>
</llsd>\n
0

There are 0 best solutions below