13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The most restrictive condition <strong>in</strong> qualify<strong>in</strong>g <strong>for</strong> <strong>XML</strong> serialization is not hav<strong>in</strong>g circularreferences. A lot of relatively complex .<strong>NET</strong> Framework classes can't be serialized to<strong>XML</strong> <strong>for</strong> this reason. Want an illustrious example? Consider the DataTable class.If you try to serialize an <strong>in</strong>stance of the DataTable class, you get a fairly unclear errormessage. Try the follow<strong>in</strong>g code:DataTable dt = new DataTable();XmlSerializer ser = new XmlSerializer(typeof(DataTable));ser.Serialize(writer, dt);The debugger stops on the constructor l<strong>in</strong>e and displays a message about a certa<strong>in</strong>error that occurred dur<strong>in</strong>g reflection of the DataTable class. Like many other <strong>Microsoft</strong>ADO.<strong>NET</strong> and <strong>XML</strong> classes, the DataTable class has circular references. For example,DataTable conta<strong>in</strong>s the Rows property, which is a collection of DataRow objects. Inturn, each DataRow object has a Table property that po<strong>in</strong>ts to the parent DataTableobject. This is clearly a circular reference, and, as such, is an appropriate justification<strong>for</strong> the run-time error.Why Is the DataSet Object <strong>XML</strong>-Serializable?The DataSet class (and the XmlNode and XmlElement classes) conta<strong>in</strong>s at least onecircular reference—specifically, the Tables collection, whose child DataTable objectsreference the parent DataSet object. Nevertheless, the DataSet object is serializablethrough the XmlSerializer class. Why is this so?The <strong>in</strong>ternal module that imports the <strong>XML</strong> schema <strong>for</strong> the type to serialize—the samemodule that does not handle circular references—specifically checks <strong>for</strong> the DataSettype. If the object turns out to be a DataSet object, the standard schema importationprocess aborts, and an alternative schema is applied. The schema importer uses themethods of the IXmlSerializable <strong>in</strong>terface to serialize and deserialize a DataSet object.The MSDN documentation only touches on the IXmlSerializable <strong>in</strong>terface, which isdef<strong>in</strong>ed <strong>in</strong> the System.Xml.Serialization namespace. This <strong>in</strong>terface is not <strong>in</strong>tended tobe used by applications—at least not yet. IXmlSerializable def<strong>in</strong>es three methods:GetSchema, ReadXml, and WriteXml. Despite their names, these ReadXml andWriteXml methods have noth<strong>in</strong>g to do with the methods we saw <strong>in</strong> Chapter 9 andChapter 10. Serialization methods are void, private, and accept only a s<strong>in</strong>gle Xml-Reader argument.You can serialize <strong>XML</strong> classes with no circular references, the default constructor, andat least one public property. If the class implements the ICollection or IEnumerable<strong>in</strong>terface, other constra<strong>in</strong>ts apply. In addition to these classes, the <strong>XML</strong> serializersupports three more classes as an exception to the previous rules: DataSet, XmlNode,and XmlElement.The XmlSerializerNamespaces ClassA few of the Serialize overloads can take an extra parameter that denotes the <strong>XML</strong>namespaces and prefixes that the XmlSerializer uses to generate qualified names. TheXmlRootAttribute class we exam<strong>in</strong>ed <strong>in</strong> the section "Configur<strong>in</strong>g the Root Node," onpage 486, is useful <strong>for</strong> def<strong>in</strong><strong>in</strong>g the default namespace but provides no way <strong>for</strong> you touse more namespaces and prefixes.The XmlSerializerNamespaces class can be used to cache multiple namespace URIsand prefixes that the target class will reference through attributes. You populate thenamespace conta<strong>in</strong>er as follows:XmlSerializer ser = new XmlSerializer(typeof(Employee));XmlSerializerNamespaces ns = new XmlSerializerNamespaces();397

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!