Data at the root level is invalid. Line 1, position 1 on valid in memory xml

5.6k views Asked by At

I have a very valid XMl string in memory downloaded from an OGC complaint web feature service.
When I use the following code to create am XmlTextReader to parse to my parser,

        using (var sr = new StringReader(schemaString))
        {
            using (var reader = new XmlTextReader(sr))
            {
                try
                {
                    schema = new GML2Parser().GetClassDefinition(reader, schema);
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                }
            }
        }

I get an exception indicating Data at rool level is invalid. If I save this string to a local file say feature_desc.xsd the use File.ReadAllText and call the aforementioned routine, I run into a similar problem.

However, if I use XmlReader.Create(feature_desc.xsd), my parser does not throw an exception when it starts traversing the XML nodes. This is a method that summarizes these actions;

 private void ParseFeatureDescription(DataTableInfo schema, string featureDescription, string featureFileName, string featureName)
        {
            var schemaLocation = string.Empty;
            if (featureFileName != null)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(featureDescription);
                schemaLocation = infrut.Utilities.CreateTempFilePath(featureFileName, FileExtension.xsd, false);
                doc.Save(schemaLocation);
            }

            var error = DeserializeTableSchema(schema, featureDescription, featureName);
            if (!string.IsNullOrEmpty(error))
            {
                var fromFileFeatureDesc = File.ReadAllText(schemaLocation);
                if (featureDescription == fromFileFeatureDesc){}
                error = DeserializeTableSchema(schema, fromFileFeatureDesc, featureName);

                if (!string.IsNullOrEmpty(error))
                {
                    // last resort
                    var reader = XmlReader.Create(schemaLocation);
                    schema = new GML2Parser().GetClassDefinition(reader, schema);
                    if (schema.Columns.Count == 0)
                    {
                        // trouble
                        ActionResponse.LogError("Error parsing description of " + featureName + ". Inner exception is \r\n" + error
                            + " " + " for content \r\n" + fromFileFeatureDesc, "WFS Worker");
                    }
                }
            }
        }

In memory representation of the string is:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ems=\"http://www.emssatcom.com\" xmlns:gml=\"http://www.opengis.net/gml\" elementFormDefault=\"qualified\" targetNamespace=\"http://www.emssatcom.com\">\r\n  <xsd:import namespace=\"http://www.opengis.net/gml\" schemaLocation=\"http://10.25.131.62:8091/geoserver/schemas/gml/2.1.2/feature.xsd\" />\r\n  <xsd:complexType name=\"asmcc_srr_viewType\">\r\n    <xsd:complexContent>\r\n      <xsd:extension base=\"gml:AbstractFeatureType\">\r\n        <xsd:sequence>\r\n          <xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"gid\" nillable=\"true\" type=\"xsd:int\" />\r\n          <xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"srr_name\" nillable=\"true\" type=\"xsd:string\" />\r\n          <xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"the_geom\" nillable=\"true\" type=\"gml:PolygonPropertyType\" />\r\n        </xsd:sequence>\r\n      </xsd:extension>\r\n    </xsd:complexContent>\r\n  </xsd:complexType>\r\n  <xsd:element name=\"asmcc_srr_view\" substitutionGroup=\"gml:_Feature\" type=\"ems:asmcc_srr_viewType\" />\r\n</xsd:schema>"

and persisted file is:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ems="http://www.emssatcom.com" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" targetNamespace="http://www.emssatcom.com">
  <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://10.25.131.62:8091/geoserver/schemas/gml/2.1.2/feature.xsd" />
  <xsd:complexType name="asmcc_srr_viewType">
    <xsd:complexContent>
      <xsd:extension base="gml:AbstractFeatureType">
        <xsd:sequence>
          <xsd:element maxOccurs="1" minOccurs="0" name="gid" nillable="true" type="xsd:int" />
          <xsd:element maxOccurs="1" minOccurs="0" name="srr_name" nillable="true" type="xsd:string" />
          <xsd:element maxOccurs="1" minOccurs="0" name="the_geom" nillable="true" type="gml:PolygonPropertyType" />
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:element name="asmcc_srr_view" substitutionGroup="gml:_Feature" type="ems:asmcc_srr_viewType" />
</xsd:schema>

Any one run into this?

1

There are 1 answers

0
vmg On

Wild guess here: sometimes I get this error (line 1 col 1) in different applications because they stored in UTF-8 encoding and they have byte order mark at the very beginning of the text/file. http://en.wikipedia.org/wiki/Byte_order_mark

Try to read file as ANSI string, not unicode