Classe XmlObjectSerializer
Assembly: System.Runtime.Serialization (in system.runtime.serialization.dll)
Extend the XmlObjectSerializer to create your own serializer to serialize and deserialize objects. Both the DataContractSerializer class and the NetDataContractSerializer class inherit from the XmlObjectSerializer and are used to serialize and deserialize objects that conform to data contract rules (objects created using the DataContractAttribute and the DataMemberAttribute).
Note per gli eredi: When you inherit from XmlObjectSerializer, you must override the following members: XmlObjectSerializer.WriteStartObject(XmlDictionaryWriter,Object), XmlObjectSerializer.WriteObjectContent(XmlDictionaryWriter,Object), XmlObjectSerializer.WriteEndObject(XmlDictionaryWriter). Additionally, the IsStartObject and ReadObject methods must be implemented for reading and deserializing.The following example shows a method named WriteObjectWithInstance that includes an XmlObjectSerializer as a parameter. The method serializes an object using either the DataContractSerializer or NetDataContractSerializer by calling the WriteObject method.
public class Test { private void WriteObjectWithInstance(XmlObjectSerializer xm, Company graph, string fileName) { // Use either the XmlDataContractSerializer or NetDataContractSerializer, // or any other class that inherits from XmlObjectSerializer to write with. Console.WriteLine(xm.GetType()); FileStream fs = new FileStream(fileName, FileMode.Create); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); xm.WriteObject(writer, graph); Console.WriteLine("Done writing {0}", fileName); } private void Run() { // Create the object to write to a file. Company graph = new Company(); graph.Name = "cohowinery.com"; // Create a DataContractSerializer and a NetDataContractSerializer. // Pass either one to the WriteObjectWithInstance method. DataContractSerializer dcs = new DataContractSerializer(typeof(Company)); NetDataContractSerializer ndcs = new NetDataContractSerializer(); WriteObjectWithInstance(dcs, graph, @"datacontract.xml"); WriteObjectWithInstance(ndcs, graph, @"netDatacontract.xml"); } [DataContract] public class Company { [DataMember] public string Name; } static void Main() { try { Console.WriteLine("Starting"); Test t = new Test(); t.Run(); Console.WriteLine("Done"); Console.ReadLine(); } catch (InvalidDataContractException iExc) { Console.WriteLine("You have an invalid data contract: "); Console.WriteLine(iExc.Message); Console.ReadLine(); } catch (SerializationException serExc) { Console.WriteLine("There is a problem with the instance:"); Console.WriteLine(serExc.Message); Console.ReadLine(); } catch (QuotaExceededException qExc) { Console.WriteLine("The quota has been exceeded"); Console.WriteLine(qExc.Message); Console.ReadLine(); } catch (Exception exc) { Console.WriteLine(exc.Message); Console.WriteLine(exc.ToString()); Console.ReadLine(); } }
System.Runtime.Serialization.XmlObjectSerializer
System.Runtime.Serialization.DataContractSerializer
System.Runtime.Serialization.NetDataContractSerializer
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
Microsoft .NET Framework 3.0 è supportato in Windows Vista, Microsoft Windows XP SP2 e Windows Server 2003 SP1.