0

我从 OGC 投诉网络功能服务下载的内存中有一个非常有效的 XMl 字符串。
当我使用以下代码创建 am 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;
                }
            }
        }

我收到一个异常,表明 rool 级别的数据无效。如果我将此字符串保存到本地文件中说 feature_desc.xsd 使用 File.ReadAllText 并调用上述例程,我会遇到类似的问题。

但是,如果我使用 XmlReader.Create(feature_desc.xsd),我的解析器在开始遍历 XML 节点时不会抛出异常。这是一种总结这些动作的方法;

 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");
                    }
                }
            }
        }

在内存中字符串的表示是:

"<?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>"

并且持久化的文件是:

<?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>

有人遇到这个吗?

4

1 回答 1

1

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

尝试将文件读取为 ANSI 字符串,而不是 unicode

于 2014-01-31T03:28:06.277 回答