Come serializzare tramite XmlSerializer (LINQ to XML)
Questo articolo illustra un esempio che serializza e deserializza tramite XmlSerializer in C# e in Visual Basic.
Esempio: Creare oggetti che contengono oggetti XElement
, quindi serializzarli e deserializzarli
Nell'esempio seguente vengono creati diversi oggetti contenenti oggetti XElement. Tali oggetti vengono quindi serializzati in un flusso di memoria e successivamente deserializzati dallo stesso flusso.
using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
public class XElementContainer
{
public XElement member;
public XElementContainer()
{
member = XLinqTest.CreateXElement();
}
public override string ToString()
{
return member.ToString();
}
}
public class XElementNullContainer
{
public XElement member;
public XElementNullContainer()
{
}
}
class XLinqTest
{
static void Main(string[] args)
{
Test<XElementNullContainer>(new XElementNullContainer());
Test<XElement>(CreateXElement());
Test<XElementContainer>(new XElementContainer());
}
public static XElement CreateXElement()
{
XNamespace ns = "http://www.adventure-works.com";
return new XElement(ns + "aw");
}
static void Test<T>(T obj)
{
using (MemoryStream stream = new MemoryStream())
{
XmlSerializer s = new XmlSerializer(typeof(T));
Console.WriteLine("Testing for type: {0}", typeof(T));
s.Serialize(XmlWriter.Create(stream), obj);
stream.Flush();
stream.Seek(0, SeekOrigin.Begin);
object o = s.Deserialize(XmlReader.Create(stream));
Console.WriteLine(" Deserialized type: {0}", o.GetType());
}
}
}
Imports System
Imports System.Xml
Imports System.Xml.Linq
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
Public Class XElementContainer
Public member As XElement
Public Sub XElementContainer()
member = XLinqTest.CreateXElement()
End Sub
Overrides Function ToString() As String
Return member.ToString()
End Function
End Class
Public Class XElementNullContainer
Public member As XElement
Public Sub XElementNullContainer()
member = Nothing
End Sub
End Class
Public Class XLinqTest
Shared Sub Main()
Test(Of XElementNullContainer)(New XElementNullContainer())
Test(Of XElement)(CreateXElement())
Test(Of XElementContainer)(New XElementContainer())
End Sub
Public Shared Function CreateXElement() As XElement
Dim ns As XNamespace = "http://www.adventure-works.com"
Return New XElement(ns + "aw")
End Function
Public Shared Sub Test(Of T)(ByRef obj)
Using stream As New MemoryStream()
Dim s As XmlSerializer = New XmlSerializer(GetType(T))
Console.WriteLine("Testing for type: {0}", GetType(T))
s.Serialize(XmlWriter.Create(stream), obj)
stream.Flush()
stream.Seek(0, SeekOrigin.Begin)
Dim o As Object = s.Deserialize(XmlReader.Create(stream))
Console.WriteLine(" Deserialized type: {0}", o.GetType())
End Using
End Sub
End Class
Nell'esempio viene prodotto l'output seguente:
Testing for type: XElementNullContainer
Deserialized type: XElementNullContainer
Testing for type: System.Xml.Linq.XElement
Deserialized type: System.Xml.Linq.XElement
Testing for type: XElementContainer
Deserialized type: XElementContainer
Collabora con noi su GitHub
L'origine di questo contenuto è disponibile in GitHub, in cui è anche possibile creare ed esaminare i problemi e le richieste pull. Per ulteriori informazioni, vedere la guida per i collaboratori.