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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The preced<strong>in</strong>g code creates an attribute override based on an element namedFamilyName. This new element is added to an attribute overrides collection. Inparticular, the FamilyName attribute overrides the LastName element on the Employeetype. The follow<strong>in</strong>g code snippet shows how to hide a source element—<strong>in</strong> this case,FirstName:SoapAttributes attrib2 = new SoapAttributes();attrib2.SoapIgnore = true;sao.Add(typeof(Employee), "FirstName", attrib2);The attribute overrides are gathered <strong>in</strong> the SoapAttributeOverrides collection, which isthen used to <strong>in</strong>itialize the SoapReflectionImporter class, as shown here, and then canbe used <strong>in</strong> the type mapp<strong>in</strong>g <strong>in</strong> the serializer:SoapReflectionImporter imp = new SoapReflectionImporter(sao);We'll return to this topic <strong>in</strong> the section "<strong>XML</strong> Serialization Attributes," on page 499. Inparticular, you'll learn how to add type <strong>in</strong><strong>for</strong>mation to pla<strong>in</strong> <strong>XML</strong> serialization, when noSOAP-encoded types are <strong>in</strong>volved.Deserializ<strong>in</strong>g <strong>XML</strong> Data to ObjectsThe deserialization process is controlled by the Deserialize method <strong>for</strong> a variety ofsources, <strong>in</strong>clud<strong>in</strong>g streams, <strong>XML</strong> readers, and text readers. Remember that by us<strong>in</strong>gthe trick discussed <strong>in</strong> Chapter 2 <strong>for</strong> <strong>XML</strong> readers (pack<strong>in</strong>g a str<strong>in</strong>g <strong>in</strong>to a Str<strong>in</strong>gReaderobject), you can also easily deserialize from str<strong>in</strong>gs.Although officially you can deserialize from streams and text readers, thedeserialization process is actually a matter of <strong>in</strong>vok<strong>in</strong>g an <strong>XML</strong> reader—more precisely,a very special breed of <strong>XML</strong> reader, optimized <strong>for</strong> serialization and <strong>for</strong> the specific class<strong>in</strong>volved. Connected to the deserialization process is the Can-Deserialize method. Thismethod returns a Boolean value <strong>in</strong>dicat<strong>in</strong>g whether the <strong>XML</strong> reader is correctlypositioned on the start element of the <strong>XML</strong> data. In addition, CanDeserialize ensuresthat the start element of the <strong>XML</strong> data is compatible with the orig<strong>in</strong>ally saved class.Normally, you call CanDeserialize <strong>in</strong> the context of a more general strategy designed totrap as many errors and exceptions as possible. If the application always deserializesdata that the <strong>XML</strong> serializer has previously created, a call to CanDeserialize can easilybe redundant. The call becomes crucial, how-ever, as soon as your application beg<strong>in</strong>sto deserialize <strong>XML</strong> data whose genu<strong>in</strong>eness and quality are not guaranteed. It is worthnot<strong>in</strong>g that CanDeserialize works only on <strong>XML</strong> readers, whereas Deserialize cansuccessfully handle streams and text readers too.From a programm<strong>in</strong>g perspective, deserializ<strong>in</strong>g is not rocket science, as the follow<strong>in</strong>gcode clearly demonstrates:StreamReader reader = new StreamReader(fileName);Employee emp = (Employee) ser.Deserialize(reader);reader.Close();Dur<strong>in</strong>g the deserialization stage, a few events can be fired. In particular, theUnknownElement, UnknownAttribute, and UnknownNode events signal when unknownand unexpected nodes are found <strong>in</strong> the <strong>XML</strong> text be<strong>in</strong>g deserialized. TheUnknownNode event is more generic than the other two and triggers regardless of thenode type on which the exception is detected. In case of unknown element or attributenodes, the UnknownNode event is fired first.404

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

Saved successfully!

Ooh no, something went wrong!