XmlRootAttribute与XmlTypeAttribute

在使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值