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

本文共 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

你可能感兴趣的文章
openGL画一个正5角星
查看>>
JDBC技术预研选型依据【转】
查看>>
我的友情链接
查看>>
celery周期性任务
查看>>
Cassandra – 理解关键概念和数据模型
查看>>
进阶PHP需要注意的一些点
查看>>
Java反射讲解-实例(1)
查看>>
Docker中安装oracle 11.2.0.4
查看>>
Uncompressing Linux... done, booting the kernel
查看>>
k8s-之无头服务初体验
查看>>
神码设备,实验配置案例(一)
查看>>
webgl学习笔记四
查看>>
【2012年给力作品】通用U盘装系统制作工具 V2.0 万能版 火热发布中……
查看>>
Spring Bean注册解析(二)
查看>>
python利用utf-8编码判断中文字符
查看>>
简单的jquery代码实现表单验证
查看>>
递归算法简单介绍
查看>>
ORA-00119 LOCAL_LISTENER错误,LRM-00109
查看>>
matlab-基础 abs 获取字符的ASCII码
查看>>
Jquery easyUI 使用资料汇总
查看>>