Xml 使用自定义架构和 IXmlSerializable 进行序列化
Xml serialization with custom schema and IXmlSerializable
我正在尝试使用 IXmlSerializable
和 XmlSchemaProviderAttribute
序列化一个对象。
架构如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="BuildingsSchema" targetNamespace="BuildingModelSchema"
elementFormDefault="qualified"
xmlns="BuildingModelSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="buildings">
<xs:complexType>
<xs:sequence>
<xs:element name="building" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
...
获取架构的代码如下所示(已找到 here):
[<Literal>]
let XML_NAMESPACE = "BuildingModelSchema"
[<Literal>]
let XSD_SCHEMA_PATH = @"BuildingsSchema.xsd"
[<XmlSchemaProvider(BuildingsSchema)>]
type BuildingsStatistics() =
interface IXmlSerializable with
...
static member BuildingsSchema(xs: XmlSchemaSet) =
let serializer = new XmlSerializer(typeof<XmlSchema>)
let schema = (serializer.Deserialize(new XmlTextReader(XSD_SCHEMA_PATH), null)) :?> XmlSchema
xs.XmlResolver <- new XmlUrlResolver()
xs.Add(schema) |> ignore
new XmlQualifiedName("buildings", XML_NAMESPACE)
...
现在,当我尝试使用 XmlSerializer
序列化一个对象时,出现异常:
{"PlanerModel.BuildingsStatistics.BuildingsSchema() must return a valid type name. Type 'buildings' cannot be found in the targetNamespace='BuildingModelSchema'."}
编辑:
在尝试反序列化有效的测试文档后,我收到了相同的错误消息。我预计 xml 模式有问题,但由于这是我第一次使用 xml 模式,我似乎没有发现错误。
进一步测试表明,XmlSerializer
初始化时抛出异常:
XmlSerializer ser = new XmlSerializer(typeof(BuildingsStatistics)); // Exception is thrown here
ser.Deserialize(...) // Will not be executed
此外,在 BuildingsSchema
方法中添加的 XmlSchema
不包含任何元素(在我的例子中,0 个元素和 14 个项目都是 'empty' (没有行号,位置等)).
我找到了问题的解决方案:我必须将架构定义更改为
<xs:complexType name="buildingsType">
<xs:sequence>
<xs:element name="building" type="buildingType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
...
和BuildingsSchema
里面返回的XmlQualifiedName
到
new XmlQualifiedName("buildingType", XML_NAMESPACE)
我正在尝试使用 IXmlSerializable
和 XmlSchemaProviderAttribute
序列化一个对象。
架构如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="BuildingsSchema" targetNamespace="BuildingModelSchema"
elementFormDefault="qualified"
xmlns="BuildingModelSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="buildings">
<xs:complexType>
<xs:sequence>
<xs:element name="building" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
...
获取架构的代码如下所示(已找到 here):
[<Literal>]
let XML_NAMESPACE = "BuildingModelSchema"
[<Literal>]
let XSD_SCHEMA_PATH = @"BuildingsSchema.xsd"
[<XmlSchemaProvider(BuildingsSchema)>]
type BuildingsStatistics() =
interface IXmlSerializable with
...
static member BuildingsSchema(xs: XmlSchemaSet) =
let serializer = new XmlSerializer(typeof<XmlSchema>)
let schema = (serializer.Deserialize(new XmlTextReader(XSD_SCHEMA_PATH), null)) :?> XmlSchema
xs.XmlResolver <- new XmlUrlResolver()
xs.Add(schema) |> ignore
new XmlQualifiedName("buildings", XML_NAMESPACE)
...
现在,当我尝试使用 XmlSerializer
序列化一个对象时,出现异常:
{"PlanerModel.BuildingsStatistics.BuildingsSchema() must return a valid type name. Type 'buildings' cannot be found in the targetNamespace='BuildingModelSchema'."}
编辑:
在尝试反序列化有效的测试文档后,我收到了相同的错误消息。我预计 xml 模式有问题,但由于这是我第一次使用 xml 模式,我似乎没有发现错误。
进一步测试表明,XmlSerializer
初始化时抛出异常:
XmlSerializer ser = new XmlSerializer(typeof(BuildingsStatistics)); // Exception is thrown here
ser.Deserialize(...) // Will not be executed
此外,在 BuildingsSchema
方法中添加的 XmlSchema
不包含任何元素(在我的例子中,0 个元素和 14 个项目都是 'empty' (没有行号,位置等)).
我找到了问题的解决方案:我必须将架构定义更改为
<xs:complexType name="buildingsType">
<xs:sequence>
<xs:element name="building" type="buildingType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
...
和BuildingsSchema
里面返回的XmlQualifiedName
到
new XmlQualifiedName("buildingType", XML_NAMESPACE)