将SerializableAttribute序列化为xml

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

1,如何将stream转化为string

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

 

有MetadataObject定义为:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
/// < 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:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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);
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值