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.

XmlAttributeOverrides object to override the serialization attributes set on the class.The XmlAttributeOverrides class is a collection and conta<strong>in</strong>s pairs consist<strong>in</strong>g of theobject types that will be overridden and the changes to apply.As shown <strong>in</strong> the follow<strong>in</strong>g code, you first create an <strong>in</strong>stance of the XmlAttributes class—that is, a helper class that conta<strong>in</strong>s all the pairs of overrid<strong>in</strong>g objects. Next you createan attribute object that is appropriate <strong>for</strong> the object be<strong>in</strong>g overridden. For example,create an XmlElementAttribute object to override a property. In do<strong>in</strong>g so, you canoptionally change the element name or the namespace. Then store the override <strong>in</strong> theXmlAttributes object. F<strong>in</strong>ally, add the XmlAttributes object to the XmlAttributeOverridesobject and <strong>in</strong>dicate the element to which all those overrides will apply.// Create the worker collection of changesXmlAttributes changes = new XmlAttributes();// Add the first override (change the element's name)XmlElementAttribute newElem = new XmlElementAttribute();newElem.ElementName = "New name";changes.XmlElements.Add(newElem);// Create the list of overridesXmlAttributeOverrides over = new XmlAttributeOverrides();// Fill the overrides list (Employee is the target class)over.Add(typeof(Employee), "Element-to-Override", changes);The <strong>in</strong>stance of the XmlAttributeOverrides class is associated with the <strong>XML</strong> serializer atcreation time, as shown here:XmlSerializer ser = new XmlSerializer(typeof(Employee), over);NoteAttribute overrid<strong>in</strong>g also enables you to use derived classes <strong>in</strong> lieuof the def<strong>in</strong>ed classes. For example, suppose you have a propertyof a certa<strong>in</strong> type. To <strong>for</strong>ce the serializer (both <strong>in</strong> serialization anddeserialization) to use a derived class, follow the steps outl<strong>in</strong>ed <strong>in</strong>the preced<strong>in</strong>g code but also set the Type property on the overrid<strong>in</strong>gelement, as shown here:// Manager is a class that <strong>in</strong>herits from EmployeenewElem.Type = typeof(Manager);Attribute overrid<strong>in</strong>g is a useful technique, and <strong>in</strong> the next section, we'll see it <strong>in</strong> action.Mapp<strong>in</strong>g SQL Server Data to ClassesIn Chapter 8, we saw the ExecuteXmlReader method exposed by the SqlCommandclass <strong>in</strong> the SQL Server–managed provider. The ExecuteXmlReader method executesa command aga<strong>in</strong>st the database and returns an <strong>XML</strong> reader if the output of thecommand can be expressed as a well-<strong>for</strong>med <strong>XML</strong> document or fragment. Let's seewhat's needed to trans<strong>for</strong>m that output <strong>in</strong>to an <strong>in</strong>stance of a class. The follow<strong>in</strong>g code isat the heart of the example. You call <strong>in</strong>to a method, the method executes an SQL <strong>XML</strong>411

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

Saved successfully!

Ooh no, something went wrong!