将SerializableAttribute序列化为xml
2010-07-15 13:47 轩脉刃 阅读(1024) 评论(0) 编辑 收藏 举报从这个例子中,你可以知道:
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/),且不得用于商业目的。如您有任何疑问或者授权方面的协商,请与我联系。