zoukankan      html  css  js  c++  java
  • 将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/),且不得用于商业目的。如您有任何疑问或者授权方面的协商,请与我联系

  • 相关阅读:
    vue Syntax Error: Unexpected token {
    MQ 分拆Json数据包然后上传
    京东商城投诉商家
    C# 读写Txt文件
    DB2时间函数 实现 时间加减
    VS恢复默认设置
    只用一次循环开销 将类似 1 A 、1 B 的数据返回成为 1 A,B 的拼接形式
    DB2 With语句递归
    属性与字段的区别
    With语句在数据统计应用
  • 原文地址:https://www.cnblogs.com/yjf512/p/1777962.html
Copyright © 2011-2022 走看看