将对象序列化为XElement并在内存中反序列化它
我想序列化一个对象到XML,但我不想保存在磁盘上。 我想把它放在一个XElementvariables(用于LINQ),然后反序列化回我的对象。
我怎样才能做到这一点?
您可以使用这两种扩展方法在XElement和对象之间进行序列化和反序列化。
public static XElement ToXElement<T>(this object obj) { using (var memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) { var xmlSerializer = new XmlSerializer(typeof(T)); xmlSerializer.Serialize(streamWriter, obj); return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray())); } } } public static T FromXElement<T>(this XElement xElement) { var xmlSerializer = new XmlSerializer(typeof(T)); return (T)xmlSerializer.Deserialize(xElement.CreateReader()); }
用法
XElement element = myClass.ToXElement<MyClass>(); var newMyClass = element.FromXElement<MyClass>();
您可以使用XMLSerialization
XML序列化是将对象的公共属性和字段转换为用于存储或传输的串行格式 (在本例中为XML)的过程。 反序列化从XML输出重新创build处于其原始状态的对象 。 您可以将序列化看作将对象的状态保存到stream或缓冲区中的一种方法。 例如,ASP.NET使用XmlSerializer类来编码XML Web服务消息
和XDocument Represents an XML document
来实现这一点
using System; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Xml.Serialization; namespace ConsoleApplication5 { public class Person { public int Age { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { XmlSerializer xs = new XmlSerializer(typeof(Person)); Person p = new Person(); p.Age = 35; p.Name = "Arnold"; Console.WriteLine("\n Before serializing...\n"); Console.WriteLine(string.Format("Age = {0} Name = {1}", p.Age,p.Name)); XDocument d = new XDocument(); using (XmlWriter xw = d.CreateWriter()) xs.Serialize(xw, p); // you can use LINQ on elm now XElement elm = d.Root; Console.WriteLine("\n From XElement...\n"); elm.Elements().All(e => { Console.WriteLine(string.Format("element name {0} , element value {1}", e.Name, e.Value)); return true; }); //deserialize back to object Person pDeserialized = xs.Deserialize((d.CreateReader())) as Person; Console.WriteLine("\n After deserializing...\n"); Console.WriteLine(string.Format("Age = {0} Name = {1}", p.Age, p.Name)); Console.ReadLine(); } } }
这里是输出
(迟到)
连载:
var doc = new XDocument(); var xmlSerializer = new XmlSerializer(typeof(MyClass)); using (var writer = doc.CreateWriter()) { xmlSerializer.Serialize(writer, obj); } // now you can use `doc`(XDocument) or `doc.Root` (XElement)
反序列化:
MyClass obj; using(var reader = doc.CreateReader()) { obj = (MyClass)xmlSerializer.Deserialize(reader); }
ToXelement没有代码分析问题,与Abdul Munim一样的答案,但解决了CA问题(CA1004除外,在这种情况下,这是不能解决的,是由devise)
public static XElement ToXElement<T>(this object value) { MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(); using (TextWriter streamWriter = new StreamWriter(memoryStream)) { memoryStream = null; var xmlSerializer = new XmlSerializer(typeof(T)); xmlSerializer.Serialize(streamWriter, value); return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray())); } } finally { if (memoryStream != null) { memoryStream.Dispose(); } } }
关于什么
public static byte[] BinarySerialize(Object obj) { byte[] serializedObject; MemoryStream ms = new MemoryStream(); BinaryFormatter b = new BinaryFormatter(); try { b.Serialize(ms, obj); ms.Seek(0, 0); serializedObject = ms.ToArray(); ms.Close(); return serializedObject; } catch { throw new SerializationException("Failed to serialize. Reason: "); } }