BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
Runtime exceptions
Serializing Collection Classes
Serializing Arrays
XmlElement Attribute Type
XmlIncludeAttribute
XmlSerializer
Serialization Attributes
Serializable Classes
Deserializing an object
Serializing an object
Serialize Class
Metadata Attributes
<< XML DOM
XQuery >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :1276

 

XmlSerializer Attributes

We have seen many how options we have to tailor a .NET class to the format of an XML type. If you are developing an application to bind XML data in a format described by an XML schema then you are in luck because you can create the classes corresponding to the schema types with the XSD tool discussed in Appendix C. However, there are quite a few alternative format description languages for XML out there that the XSD tool cannot convert into classes, DTDs and Relax-NG for example. If you need to develop classes to map types from schema formats other than XSD and XDR then this section is for you! It is intended to provide quick answers to “What attribute do I need to map this code to my XML”-type questions when you have to develop serialization classes “by hand”. The left column shows C# code fragments with serialization attributes for class definitions, the right column shows how the XmlSerializer maps this code construct to XML. Remember that the attributes’ properties are optional in most cases.

1.2            Table 8.1 Metadata attributes control how the XmlSerializer maps classes to XML documents. Each attribute allows further customization of the XML format through its properties.

Code with Metadata Attributes

XML format

[XmlRoot( ElementName=”Automobile”,

    Namespace=”urn:my-ns”,

    IsNullable=”true”))]

public class Car {

 }

<Automobile xmlns=”urn:my-ns”>

</Automobile>

public class Car {

   [XmlElement(ElementName=”CarMake”,
  Namespace=”urn:my-ns”,

  IsNullable=”true”]

   public string Make;

 }

<Car>

  <CarMake xmlns=”urn:my-ns”></CarMake>

</Car>

or if Make = null

<Car>

  <CarMake xsi:nil=”true” />

</Car>

Public class ParkingLot {
  [XmlElement]

  public Car[] Cars;

  public Car[] MoreCars;

}

<ParkingLot>

  <! -- no parent for the array elements - ->

  <Car></Car>

  <! - - enclosing element for the array elements - - >

  <MoreCars>

    <Car></Car>

    <Car></Car>

  </MoreCars>

</ParkingLot>

public class Car {

  [XmlAttribute(

    AttributeName=”CarMake”,

    Namespace=”urn:my-ns”)]

  public string Make;

… }

<Car n1:CarMake=”” xmlns:p1=”urn:my-ns”>

</Car>

public class Car {

   [XmlIgnore]

   public string Make

… }

<Car>

</Car>

public class Car {

  public string Make = “Ford”;

  [XmlText]

  public string Desc = “sedan”;

}

<Car>

<Make>Ford</Make>

sedan

</Car>

public class ParkingLot {

  [XmlArray(

    ElementName=”ParkedCars”,
    Namespace=”urn:my-ns”,

    IsNullable=”true”)]

  public Car[] Cars;

}

<ParkingLot>

  <ParkedCars xmlns=”urn:my-ns”>

    <Car>…</Car>

    <Car xsi:nil=”true” />

  </ParkedCars>

</ParkingLot>

public class ParkingLot {

  [XmlArrayItem(

    ElementName=”ParkedCar”,
    Namespace=”urn:my-ns”,

    IsNullable=”true”)]

  public Car[] Cars;

… }

<ParkingLot>

  <Cars>

    <ParkedCar><ParkedCar>

    <ParkedCar xsi:nil=”true”/>

  </Cars>

</ParkingLot>

public enum Makes {

  [XmlEnum(“FCar”)]

  Ford,

  Toyota

}

public class Car {

   public Makes Make;

}

If Make == Makes.Ford :

<Car>
  <Make>FCar</Make>

</Car>

If Make == Makes.Toyota :

<Car>
  <Make>Toyota</Make>

</Car>

public class Car

{

  [XmlAnyElement]

  XmlElement[] extraElements;

}

extraElements contains 2 nodes: Color and Wheels:

<Car>

   <Color>Red</Color>
   <Wheels>Alloy</Wheels>
</Car>

public class Car

{

  [XmlAnyAttribute]

  XmlAttribute[] extraAttribs;

}

ExtraAttribs contains 2 attributes: vin and miles

<Car vin=”12335” miles=”123”>
</Car>

public class Car

{

  [DefaultValueAttribute(“Ford”)]

  public string Make;

}

If Make == “Ford”:

<Car>

  <!- -  default has to be derived from the schema -->

</Car>

Otherwise:

<Car>

  <Make>Toyota</Make>

</Car>

public class Car {

  [XmlNamespaceDeclarations]

  public XmlSerializerNamespaces

    namespaces;

}

namespaces maps the prefix c to “urn:christoph-cars”

<Car

  xmlns:c ="urn:christoph-cars">

</Car>

[XmlType(

  TypeName="Car_T",

  Namespace="urn:christoph-car")]

public class Car

{

  public string Make;

}

<Car_T>

  <Make xmlns="urn:christoph-car">

  </Make>

</Car_T>


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En
A Greate Opportunituy as a SQL Deve
An immediate job opportunity as a B

View all Jobs (Add yours)
View all CV (Add yours)



chicago web site design
conference calling
Host Department NOLIMIT Web Hosting
0800 Numbers
Diesel sunglasses
NJ Pool Contractor
answering service
online fax service
swimming pool contractor
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP