C# and XML Primer by Jonathan Hartwell

C# and XML Primer by Jonathan Hartwell

Author:Jonathan Hartwell
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


The XML looks no different than what we would have created using XmlDocument or LINQ to XML, yet it took only a few lines of code to do so. You may have noticed the namespaces on the Book element. Since those are typically the default namespaces, the XmlSerializer will add those to the root element. Another thing to note is that the root element is the name of our model type.

So how does this work under the hood? The XmlSerializer uses reflection to get the properties of a given serializable class and then creates elements based on those properties. Since this is all handled by reflection, there are several attributes that can be used to iterate on that will perform various actions. For instance, there is the XmlAttribute class , which is used on a property that should have some sort of attributes. There is also an XmlIgnore attribute, which can be used to indicate that a specific property should not be serialized. In Chapter 5, we will create our own XmlSerializer so that we can get a better understanding of exactly how it works.

I just gave examples of other attributes that can be used in a given model. Let's use both of them and see what the output is. We will revisit our book model again. This time, let's not serialize the year that the book was written and let's also add a propety to the name to indicate that a movie has been made based on this book. Our Book class now changes to the following model:

[Serializable]

public class Book

{

[XmlAttribute(AttributeName = "Movie")]

public bool IsMovie { get; set; }

public string Title { get; set; }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.