从这个例子中,你可以知道:
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);
}