博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XmlRootAttribute与XmlTypeAttribute
阅读量:5747 次
发布时间:2019-06-18

本文共 8175 字,大约阅读时间需要 27 分钟。

在使用xml进行对象与字符串序列化与反序列化时,遇到如下问题:

 

 

ContractedBlock.gif
ExpandedBlockStart.gif
代码
 
[System.CodeDom.Compiler.GeneratedCodeAttribute(
"
xsd
"
,
"
2.0.50727.1432
"
)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(
"
code
"
)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType
=
true
, Namespace
=
"
http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition
"
)]
[System.Xml.Serialization.XmlRootAttribute(Namespace
=
"
http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition
"
, IsNullable
=
false
)]
public
partial
class
Report : IReport
{
private
object
[] itemsField;
private
ReportItemsElementNameType[] itemsElementNameField;
private
System.Xml.XmlAttribute[] anyAttrField;
///
<remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
[System.Xml.Serialization.XmlElementAttribute(
"
Author
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
AutoRefresh
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
Classes
"
,
typeof
(ClassesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
Code
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
CodeModules
"
,
typeof
(CodeModulesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
ConsumeContainerWhitespace
"
,
typeof
(
bool
))]
[System.Xml.Serialization.XmlElementAttribute(
"
CustomProperties
"
,
typeof
(CustomPropertiesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataElementName
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataElementStyle
"
,
typeof
(ReportDataElementStyle))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataSchema
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataSets
"
,
typeof
(DataSetsType))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataSources
"
,
typeof
(DataSourcesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataTransform
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
DeferVariableEvaluation
"
,
typeof
(
bool
))]
[System.Xml.Serialization.XmlElementAttribute(
"
Description
"
,
typeof
(StringLocIDType))]
[System.Xml.Serialization.XmlElementAttribute(
"
EmbeddedImages
"
,
typeof
(EmbeddedImagesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
InitialPageName
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
Language
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
ReportParameters
"
,
typeof
(ReportParametersType))]
[System.Xml.Serialization.XmlElementAttribute(
"
ReportSections
"
,
typeof
(ReportSectionsType))]
[System.Xml.Serialization.XmlElementAttribute(
"
Variables
"
,
typeof
(VariablesType))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute(
"
ItemsElementName
"
)]
public
object
[] Items
{
get
{
return
this
.itemsField;
}
set
{
this
.itemsField
=
value;
}
}
///
<remarks/>
[System.Xml.Serialization.XmlElementAttribute(
"
ItemsElementName
"
)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public
ReportItemsElementNameType[] ItemsElementName
{
get
{
return
this
.itemsElementNameField;
}
set
{
this
.itemsElementNameField
=
value;
}
}
///
<remarks/>
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public
System.Xml.XmlAttribute[] AnyAttr
{
get
{
return
this
.anyAttrField;
}
set
{
this
.anyAttrField
=
value;
}
}
}

 

Report类有XmlRootAttributeXmlTypeAttribute这2个特性,而另一个类MapPolygonLayerType则只有XmlTypeAttribute 

一个特性,如下所示:

 

 

ContractedBlock.gif
ExpandedBlockStart.gif
代码
 
[System.CodeDom.Compiler.GeneratedCodeAttribute(
"
xsd
"
,
"
2.0.50727.1432
"
)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(
"
code
"
)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace
=
"
http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition
"
)]
public
partial
class
MapPolygonLayerType : IMapPolygonLayerType
{
private
object
[] itemsField;
private
MapPolygonLayerTypeItemsElementNameType[] itemsElementNameField;
private
string
nameField;
private
System.Xml.XmlAttribute[] anyAttrField;
///
<remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
[System.Xml.Serialization.XmlElementAttribute(
"
DataElementName
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
DataElementOutput
"
,
typeof
(MapPolygonLayerTypeDataElementOutput))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapBindingFieldPairs
"
,
typeof
(MapBindingFieldPairsType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapCenterPointRules
"
,
typeof
(MapPointRulesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapDataRegionName
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapFieldDefinitions
"
,
typeof
(MapFieldDefinitionsType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapMarkerTemplate
"
,
typeof
(MapMarkerTemplateType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapPolygonRules
"
,
typeof
(MapPolygonRulesType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapPolygonTemplate
"
,
typeof
(MapPolygonTemplateType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapPolygons
"
,
typeof
(MapPolygonsType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapShapefile
"
,
typeof
(MapShapefileType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapSpatialDataRegion
"
,
typeof
(MapSpatialDataRegionType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MapSpatialDataSet
"
,
typeof
(MapSpatialDataSetType))]
[System.Xml.Serialization.XmlElementAttribute(
"
MaximumZoom
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
MinimumZoom
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
Transparency
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlElementAttribute(
"
VisibilityMode
"
,
typeof
(
string
))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute(
"
ItemsElementName
"
)]
public
object
[] Items
{
get
{
return
this
.itemsField;
}
set
{
this
.itemsField
=
value;
}
}
///
<remarks/>
[System.Xml.Serialization.XmlElementAttribute(
"
ItemsElementName
"
)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public
MapPolygonLayerTypeItemsElementNameType[] ItemsElementName
{
get
{
return
this
.itemsElementNameField;
}
set
{
this
.itemsElementNameField
=
value;
}
}
///
<remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType
=
"
normalizedString
"
)]
public
string
Name
{
get
{
return
this
.nameField;
}
set
{
this
.nameField
=
value;
}
}
///
<remarks/>
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public
System.Xml.XmlAttribute[] AnyAttr
{
get
{
return
this
.anyAttrField;
}
set
{
this
.anyAttrField
=
value;
}
}
}

 我们对其序列化和反序列化:

 

 

 

ContractedBlock.gif
ExpandedBlockStart.gif
代码
 
string
mapline
=
SimpleLoadXml(
@"
..\..\..\MapLineRules.xml
"
);
Report ret2
=
(Report)DeSerialize(Encoding.UTF8.GetBytes(mapline),
typeof
(Report));
string
ret2str
=
SerializeToStr(ret2);
string
mapPolygonLayerstr
=
"
<MapPolygonLayerType Name=\
"
Name025c5c62b72241e79fc7f18b00978e4a\
"
xmlns:xsi=\
"
http:
//
www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"
http://www.w3.org/2001/XMLSchema
\">" +
"
<MapCenterPointRules xmlns=\
"
http:
//
schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition\">" +
"
<MapMarkerRule>
"
+
"
<MapMarkers>
"
+
"
<MapMarker />
"
+
"
</MapMarkers>
"
+
"
</MapMarkerRule>
"
+
"
</MapCenterPointRules>
"
+
"
</MapPolygonLayerType>
"
;
object
mapPolygonLayerobj
=
DeSerialize(mapPolygonLayerstr,
typeof
(MapPolygonLayerType));
string
mapPolygonLayerstr2
=
"
<MapPolygonLayerType Name=\
"
Name025c5c62b72241e79fc7f18b00978e4a\
"
>
"
+
"
<MapCenterPointRules>
"
+
"
<MapMarkerRule>
"
+
"
<MapMarkers>
"
+
"
<MapMarker />
"
+
"
</MapMarkers>
"
+
"
</MapMarkerRule>
"
+
"
</MapCenterPointRules>
"
+
"
</MapPolygonLayerType>
"
;
object
mapPolygonLayerobj2
=
DeSerialize(mapPolygonLayerstr2,
typeof
(MapPolygonLayerType));

 

 

 

ret2的结构为:

 

 

2010062217342686.png 

 

 

 

 mapPolygonLayerobj的结构为:

2010062217345294.png

 mapPolygonLayerobj2的结构为:

 

 

 

 

 

2010062217352873.png

 

 

 

 

 

可见mapPolygonLayerobj与ret2中的MapPolygonLayer结构一致,而mapPolygonLayerobj2的结构则不相同。

原因在于mapPolygonLayerstr比mapPolygonLayerstr2多了xmlns:xsi, xmlns:xsd,xmlns这3个属性(注意属性

所在的位置。

总上,XmlRootAttribute与XmlTypeAttribute差别不大。

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/jenneyblog/archive/2010/06/22/1762855.html

你可能感兴趣的文章
Nagios 监控实例部署
查看>>
实时视频直播客户端技术盘点:Native、HTML5、WebRTC、微信小程序
查看>>
Microsoft System Center 2012部署(二)
查看>>
<转>CentOS / Redhat: Install KVM Virtualization ...
查看>>
原生android VS 定制android(一)
查看>>
修改Linux系统下的最大文件描述符限制
查看>>
CakePHP 2.x CookBook 中文版 第一章 欢迎
查看>>
Druid 在小米公司部分技术实践
查看>>
LNMP - 常见的502错误
查看>>
配置DNS服务器
查看>>
server2008R2WSUS部署 先决条件
查看>>
Lotus Notes压缩数据库的方法
查看>>
修复Bug好比钓鱼
查看>>
php过滤所有英文中文的标点符号代码
查看>>
ssh+chroot -- 给ssh上把锁
查看>>
C语言通过串口发送AT指令
查看>>
Mac上php和mysql的安装以及一些配置问题解决
查看>>
如何做项目或软件产品计划
查看>>
基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理
查看>>
CentOS 在编译php 的时候可能出现的错误以及需要安装的类库
查看>>