• 将SerializableAttribute序列化为xml


    从这个例子中,你可以知道:

    1,如何将stream转化为string

    2,如何将searializableAttribute属性序列化为xml

    有MetadataObject定义为:

    代码
    /// <remarks/>
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(ActionParameter))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Action))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(AssociationGroup))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(TypeDescriptor))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Parameter))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(FilterDescriptor))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Identifier))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(LobSystemInstance))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(IndividuallySecurableMetadataObject))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(MethodInstance))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Association))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Method))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Entity))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(LobSystem))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Model))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.microsoft.com/windows/2007/BusinessDataCatalog")]
    public abstract partial class MetadataObject
    {

    private LocalizedDisplayName[] localizedDisplayNamesField;

    private Property[] propertiesField;

    private string nameField;

    private string defaultDisplayNameField;

    private bool isCachedField;

    public MetadataObject()
    {
    this.isCachedField = true;
    }

    ///
    <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = false)]
    public LocalizedDisplayName[] LocalizedDisplayNames
    {
    get
    {
    return this.localizedDisplayNamesField;
    }
    set
    {
    this.localizedDisplayNamesField = value;
    }
    }

    ///
    <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = false)]
    public Property[] Properties
    {
    get
    {
    return this.propertiesField;
    }
    set
    {
    this.propertiesField = value;
    }
    }

    ///
    <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name
    {
    get
    {
    return this.nameField;
    }
    set
    {
    this.nameField = value;
    }
    }

    ///
    <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string DefaultDisplayName
    {
    get
    {
    return this.defaultDisplayNameField;
    }
    set
    {
    this.defaultDisplayNameField = value;
    }
    }

    ///
    <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    [System.ComponentModel.DefaultValueAttribute(true)]
    public bool IsCached
    {
    get
    {
    return this.isCachedField;
    }
    set
    {
    this.isCachedField = value;
    }
    }
    }

    下面一个函数将其转化为xml的string:

    代码
    public static string GetXmlFromModel(MetadataObject metadataObject)
    {
    MemoryStream ms = new MemoryStream();
    XmlSerializer serializer = new XmlSerializer(metadataObject.GetType());
    serializer.Serialize(ms, metadataObject);
    return System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray(), 0, ms.ToArray().Length);
    }

    实时了解作者更多技术文章,技术心得,请关注微信公众号“轩脉刃的刀光剑影”

    本文基于署名-非商业性使用 3.0许可协议发布,欢迎转载,演绎,但是必须保留本文的署名叶剑峰(包含链接http://www.cnblogs.com/yjf512/),且不得用于商业目的。如您有任何疑问或者授权方面的协商,请与我联系

  • 相关阅读:
    Object-c的类可以多重继承么?可以实现多个接口么?如何实现?
    对于TableViewCell重用机制的理解
    xcode快捷方式
    Mysql数据迁移——按分号split一列字段插入另一张表
    Android手机导出微信聊天记录
    Java性能分析工具之Jprofiler初体验
    Android adb端口被占用的解决办法
    mysql limit查询性能优化
    Delphi异或算法转换为java实现
    [python]用Python进行SQLite数据库操作
  • 原文地址:https://www.cnblogs.com/yjf512/p/1777962.html
Copyright © 2020-2023  润新知