XML serialization using XMLSerializer

xiaoxiao2021-03-05  21

Write XmlSerializer serializer = new XmlSerializer (typeof (PurchaseOrder)); TextWriter writer = new StreamWriter (filename); PurchaseOrder po = new PurchaseOrder (); serializer.Serialize (writer, po); writer.Close (); read XmlSerializer serializer = new XmlSerializer (typeof (PurchaseOrder)); / * If the XML document has been altered with unknown nodes or attributes, handle them with the UnknownNode and UnknownAttribute events * / serializer.UnknownNode = new XmlNodeEventHandler (serializer_UnknownNode);. serializer.UnknownAttribute = new XmlAttributeEventHandler (serializer_UnknownAttribute); // A FileStream is needed to read the XML document.FileStream fs = new FileStream (filename, FileMode.Open); // Declare an object variable of the type to be deserialized.PurchaseOrder po; / * Use the Deserialize Method to restore the object's state withdata from the xml document. * / po = (purchaseorder) Serializer.deSerialize (fs);

protected void serializer_UnknownNode (object sender, XmlNodeEventArgs e) {Console.WriteLine ( "Unknown Node:" e.Name "/ t" e.Text);} protected void serializer_UnknownAttribute (object sender, XmlAttributeEventArgs e) {System.Xml .Xmlattribute attr = e.attr; console.writeline ("Unknown Attribute" Attr.Name "= '" Attr.Value "'");} Serestribute class [XMLRootttribute ("PurchaseOrder", Namespace = "http://www.cpandl.com", isnullable = false] public class purchaseorder {Reference: MSDN- XMLSerializer class ms-help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/cpref/html /FRLRFSYSTEMXMLSERIALIZATIONXMLSERIALIZERCLASSTOPIC.HTM

转载请注明原文地址:https://www.9cbs.com/read-32744.html

New Post(0)