C# 在XML反序列化中使用继承
我正在尝试用继承反序列化 例如,我有一门课程是为 简而言之,XSD是这样的:C# 在XML反序列化中使用继承,c#,xml,serialization,xsd,C#,Xml,Serialization,Xsd,我正在尝试用继承反序列化 例如,我有一门课程是为 简而言之,XSD是这样的: <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://a9.com/-/spec/opensearch/1.1/" xmlns:xs="http://www.w3.org/200
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://a9.com/-/spec/opensearch/1.1/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="OpenSearchDescription">
<xs:complexType>
<xs:sequence>
<xs:element name="ShortName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
[System.SerializableAttribute]
[XmlRoot(Namespace = "http://www.mozilla.org/2006/browser/search/", IsNullable = false, ElementName = "SearchPlugin")]
public class SearchPlugin
{
public OpenSearch OpenSearch { get; set; }
}
当我尝试反序列化一个xml时,这很好用
对于我的mozilla实现,我只需要以下内容:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://a9.com/-/spec/opensearch/1.1/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="OpenSearchDescription">
<xs:complexType>
<xs:sequence>
<xs:element name="ShortName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
[System.SerializableAttribute]
[XmlRoot(Namespace = "http://www.mozilla.org/2006/browser/search/", IsNullable = false, ElementName = "SearchPlugin")]
public class SearchPlugin
{
public OpenSearch OpenSearch { get; set; }
}
但是,每当我尝试反序列化SearchPlugin对象时,OpenSearch对象就是空的
我该怎么做呢?我试图通过使用xsd.exe生成代码来为自己创建一个示例,但是当我尝试为SearchPlugin.xsd生成代码时,也不断出现错误,因此这没有帮助…我将从您尝试反序列化的XML开始,从该XML中获取xsd文件,如果需要,调整xsd以确保它们有效,然后使用xsd.exe生成类。通过查看生成的类,您将看到缺少了什么 真正熟悉xsd.exe的人会立即指出SearchPlugin类的OpenSearch属性没有XmlElementAttribute,类似于
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
我相信这就是你得到空值的原因。下面是我将如何排除故障
从XML开始;基于您的模式和类,我假设您期望如下所示:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<SearchPlugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.mozilla.org/2006/browser/search/">
<os:OpenSearchDescription>
<os:ShortName>ShortName1</os:ShortName>
</os:OpenSearchDescription>
</SearchPlugin>
生成的类:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.mozilla.org/2006/browser/search/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mozilla.org/2006/browser/search/", IsNullable=false)]
public partial class SearchPlugin {
private OpenSearchDescription openSearchDescriptionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
public OpenSearchDescription OpenSearchDescription {
get {
return this.openSearchDescriptionField;
}
set {
this.openSearchDescriptionField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://a9.com/-/spec/opensearch/1.1/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/", IsNullable=false)]
public partial class OpenSearchDescription {
private string shortNameField;
/// <remarks/>
public string ShortName {
get {
return this.shortNameField;
}
set {
this.shortNameField = value;
}
}
}
使用System.Xml.Serialization;
//
//此源代码由xsd自动生成,版本=4.0.30319.1。
//
///
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://www.mozilla.org/2006/browser/search/")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://www.mozilla.org/2006/browser/search/“,IsNullable=false)]
公共部分类搜索插件{
私有OpenSearchDescription OpenSearchDescription字段;
///
[System.Xml.Serialization.XmlElementAttribute(命名空间=”http://a9.com/-/spec/opensearch/1.1/")]
公共OpenSearchDescription OpenSearchDescription{
得到{
返回此.openSearchDescriptionField;
}
设置{
this.openSearchDescriptionField=值;
}
}
}
///
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://a9.com/-/spec/opensearch/1.1/")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://a9.com/-/spec/opensearch/1.1/“,IsNullable=false)]
公共部分类OpenSearchDescription{
私有字符串短名称字段;
///
公共字符串短名称{
得到{
返回此.shortNameField;
}
设置{
this.shortNameField=值;
}
}
}
我将从您尝试反序列化的XML开始,从该XML中获取XSD文件,如果需要,调整XSD以确保其有效,然后使用XSD.exe生成类。通过查看生成的类,您将看到缺少了什么
真正熟悉xsd.exe的人会立即指出SearchPlugin类的OpenSearch属性没有XmlElementAttribute,类似于
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
我相信这就是你得到空值的原因。下面是我将如何排除故障
从XML开始;基于您的模式和类,我假设您期望如下所示:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<SearchPlugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.mozilla.org/2006/browser/search/">
<os:OpenSearchDescription>
<os:ShortName>ShortName1</os:ShortName>
</os:OpenSearchDescription>
</SearchPlugin>
生成的类:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.mozilla.org/2006/browser/search/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mozilla.org/2006/browser/search/", IsNullable=false)]
public partial class SearchPlugin {
private OpenSearchDescription openSearchDescriptionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
public OpenSearchDescription OpenSearchDescription {
get {
return this.openSearchDescriptionField;
}
set {
this.openSearchDescriptionField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://a9.com/-/spec/opensearch/1.1/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://a9.com/-/spec/opensearch/1.1/", IsNullable=false)]
public partial class OpenSearchDescription {
private string shortNameField;
/// <remarks/>
public string ShortName {
get {
return this.shortNameField;
}
set {
this.shortNameField = value;
}
}
}
使用System.Xml.Serialization;
//
//此源代码由xsd自动生成,版本=4.0.30319.1。
//
///
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://www.mozilla.org/2006/browser/search/")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://www.mozilla.org/2006/browser/search/“,IsNullable=false)]
公共部分类搜索插件{
私有OpenSearchDescription OpenSearchDescription字段;
///
[System.Xml.Serialization.XmlElementAttribute(命名空间=”http://a9.com/-/spec/opensearch/1.1/")]
公共OpenSearchDescription OpenSearchDescription{
得到{
返回此.openSearchDescriptionField;
}
设置{
this.openSearchDescriptionField=值;
}
}
}
///
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://a9.com/-/spec/opensearch/1.1/")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://a9.com/-/spec/opensearch/1.1/“,IsNullable=false)]
公共部分类OpenSearchDescription{
私有字符串短名称字段;
///
公共字符串短名称{
得到{
返回此.shortNameField;
}
设置{
this.shortNameField=值;
}
}
}