简体   繁体   English

根级别的数据无效。 第1行,内存xml中有效的位置1

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

I have a very valid XMl string in memory downloaded from an OGC complaint web feature service. 我从OGC投诉网络功能服务下载的内存中有一个非常有效的XMl字符串。
When I use the following code to create am XmlTextReader to parse to my parser, 当我使用以下代码创建XmlTextReader解析到解析器时,

        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. 如果我将此字符串保存到本地文件中,例如,使用File.ReadAllText使用feature_desc.xsd并调用上述例程,则会遇到类似的问题。

However, if I use XmlReader.Create(feature_desc.xsd), my parser does not throw an exception when it starts traversing the XML nodes. 但是,如果使用XmlReader.Create(feature_desc.xsd),则解析器开始遍历XML节点时不会引发异常。 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? 有人碰到这个吗?

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. 在这里进行疯狂的猜测:有时我在不同的应用程序中遇到此错误(第1行第1行),因为它们以UTF-8编码存储,并且在文本/文件的开头有字节顺序标记。 http://en.wikipedia.org/wiki/Byte_order_mark http://en.wikipedia.org/wiki/Byte_order_mark

Try to read file as ANSI string, not unicode 尝试将文件读取为ANSI字符串而不是unicode

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Xml.XmlException:根级别的数据无效。 第 1 行,position 1 - 有效的 XML 文件? - System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1 - Valid XML file? “根级别的数据无效。第 1 行,position 1”解析 XML 时 - "Data at the root level is invalid. Line 1, position 1" When Parsing XML 根级别的数据无效。 第1行在XML中的位置1 - data at the root level is invalid. line 1 position 1 in XML xml.LoadData - 根级别的数据无效。 1号线,position 1 - xml.LoadData - Data at the root level is invalid. Line 1, position 1 根级别的数据无效。 第1行,位置1 - Data at the root level is invalid. Line 1, position 1 URL 中的 XML - 根级别的数据无效。 第 1 行,位置 1 为什么它适用于一个 URL 而不是另一个? - XML from URL - Data at the root level is invalid. Line 1, position 1 Why it works with one URL and not the other? C# XML 解析 - 根级别的数据无效。 第 1 行,位置 1 - C# XML Parsing - Data at the root level is invalid. Line 1, position 1 使用C#根据XSD验证XML:根级别的数据无效。 第1行,位置1 - Validating XML against XSD using C#: Data at the root level is invalid. Line 1, position 1 SolrNET-根级别的数据无效。 第1行,位置1 - SolrNET - Data at the root level is invalid. Line 1, position 1 错误加载XML文件{“根级别的数据无效。 第1行,位置1。”} - Error Loading XML File {“Data at the root level is invalid. Line 1, position 1.”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM