C# 如何将对象序列化为XML
如何将.net对象序列化为XML,然后将其反序列化回来?在VB.net中,您首先要向类添加一些属性(如果需要的话)C# 如何将对象序列化为XML,c#,.net,asp.net,xml,vb.net,C#,.net,Asp.net,Xml,Vb.net,如何将.net对象序列化为XML,然后将其反序列化回来?在VB.net中,您首先要向类添加一些属性(如果需要的话) 这可以通过对象和字符串扩展方法实现 using System.IO; using System.Xml.Serialization; namespace XmlExtensionMethods { /// <summary> /// Provides extension methods useful for XML Serialization and deseria
这可以通过对象和字符串扩展方法实现
using System.IO;
using System.Xml.Serialization;
namespace XmlExtensionMethods
{
/// <summary>
/// Provides extension methods useful for XML Serialization and deserialization.
/// </summary>
public static class XMLExtensions
{
/// <summary>
/// Extension method that takes objects and serialized them.
/// </summary>
/// <typeparam name="T">The type of the object to be serialized.</typeparam>
/// <param name="source">The object to be serialized.</param>
/// <returns>A string that represents the serialized XML.</returns>
public static string SerializeXML<T>(this T source) where T : class, new()
{
source.CheckNull("Object to be serialized.");
var serializer = new XmlSerializer(typeof(T));
using (var writer = new StringWriter())
{
serializer.Serialize(writer, source);
return writer.ToString();
}
}
/// <summary>
/// Extension method to string which attempts to deserialize XML with the same name as the source string.
/// </summary>
/// <typeparam name="T">The type which to be deserialized to.</typeparam>
/// <param name="XML">The source string</param>
/// <returns>The deserialized object, or null if unsuccessful.</returns>
public static T DeserializeXML<T>(this string XML) where T : class, new()
{
XML.CheckNull("XML-string");
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(XML))
{
try { return (T)serializer.Deserialize(reader); }
catch { return null; } // Could not be deserialized to this type.
}
}
}
}
}
使用System.IO;
使用System.Xml.Serialization;
命名空间XmlExtensionMethods
{
///
///提供对XML序列化和反序列化有用的扩展方法。
///
公共静态类XMLExtensions
{
///
///获取对象并将其序列化的扩展方法。
///
///要序列化的对象的类型。
///要序列化的对象。
///表示序列化XML的字符串。
公共静态字符串序列化XML(此T源),其中T:class,new()
{
CheckNull(“要序列化的对象”);
var serializer=newxmlserializer(typeof(T));
使用(var writer=new StringWriter())
{
serializer.Serialize(writer,source);
返回writer.ToString();
}
}
///
///尝试反序列化与源字符串同名的XML的字符串扩展方法。
///
///要反序列化到的类型。
///源字符串
///反序列化对象,如果不成功,则为null。
公共静态T反序列化XML(此字符串XML),其中T:class,new()
{
CheckNull(“XML字符串”);
var serializer=newxmlserializer(typeof(T));
使用(var reader=newstringreader(XML))
{
尝试{return(T)serializer.Deserialize(reader);}
无法将catch{return null;}//反序列化为此类型。
}
}
}
}
}
您可以这样使用它:
string savestring;
public void Save()
{
savestring = mySerializableObject.SerializeXML<MySerializableClass>();
}
public void Load()
{
mySerializableObject = savestring.DeserializeXML<MySerializableClass>();
}
string保存字符串;
公共作废保存()
{
savestring=mySerializableObject.SerializeXML();
}
公共空荷载()
{
mySerializableObject=savestring.DeserializeXML();
}
注:我不认为这些扩展方法有什么好处。VB.NET
using System.IO;
using System.Xml.Serialization;
namespace XmlExtensionMethods
{
/// <summary>
/// Provides extension methods useful for XML Serialization and deserialization.
/// </summary>
public static class XMLExtensions
{
/// <summary>
/// Extension method that takes objects and serialized them.
/// </summary>
/// <typeparam name="T">The type of the object to be serialized.</typeparam>
/// <param name="source">The object to be serialized.</param>
/// <returns>A string that represents the serialized XML.</returns>
public static string SerializeXML<T>(this T source) where T : class, new()
{
source.CheckNull("Object to be serialized.");
var serializer = new XmlSerializer(typeof(T));
using (var writer = new StringWriter())
{
serializer.Serialize(writer, source);
return writer.ToString();
}
}
/// <summary>
/// Extension method to string which attempts to deserialize XML with the same name as the source string.
/// </summary>
/// <typeparam name="T">The type which to be deserialized to.</typeparam>
/// <param name="XML">The source string</param>
/// <returns>The deserialized object, or null if unsuccessful.</returns>
public static T DeserializeXML<T>(this string XML) where T : class, new()
{
XML.CheckNull("XML-string");
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(XML))
{
try { return (T)serializer.Deserialize(reader); }
catch { return null; } // Could not be deserialized to this type.
}
}
}
}
}
Public Shared Function SerializeObj(obj As Object) As String
Dim xs As New System.Xml.Serialization.XmlSerializer(obj.GetType)
Dim w As New IO.StringWriter
xs.Serialize(w, obj)
Return w.ToString
End Function
往回看有点复杂,您需要声明一个xml文档并从中提取数据 您可以查看此链接,它可能会帮助您解答所有问题。谢谢
Public Shared Function SerializeObj(obj As Object) As String
Dim xs As New System.Xml.Serialization.XmlSerializer(obj.GetType)
Dim w As New IO.StringWriter
xs.Serialize(w, obj)
Return w.ToString
End Function