zoukankan      html  css  js  c++  java
  • XmlRootAttribute与XmlTypeAttribute

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

    代码
    [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 

    一个特性,如下所示:

     

    代码
    [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;
    }
    }
    }

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

    代码
    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的结构为:

     

     

     

     mapPolygonLayerobj的结构为:

     mapPolygonLayerobj2的结构为:

     

     

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

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

    所在的位置。

    总上,XmlRootAttribute与XmlTypeAttribute差别不大。

  • 相关阅读:
    selenium学习笔记05-selenium操作from表单
    selenium学习笔记04-webdriver核心方法的属性和使用
    selenium学习笔记03-selenium webdriver工作原理
    selenium学习笔记02-selenium定位的八大方法
    selenium学习笔记01
    微服务质量保证学习笔记(一)
    pytest,setup和teardown
    pytest用例编写规则、执行测试
    速耀达账套备份与恢复(速达二次开发)
    速耀达系统权限设置(速达二次开发)
  • 原文地址:https://www.cnblogs.com/jenneyblog/p/1762855.html
Copyright © 2011-2022 走看看