Figure 2 Typical XML Schema Document
<xsd:schema
targetNamespace="uuid:048b2fa1-d557-473f-ba4c-acee78fe3f7d"
xmlns:target="uuid:048b2fa1-d557-473f-ba4c-acee78fe3f7d"
xmlns:xsd="https://www.w3.org/2001/XMLSchema"
>
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:choice>
<xsd:element name="name" type="xsd:string"
xsi:nillable="true" />
<xsd:element name="id" type="xsd:string" />
</xsd:choice>
<xsd:any processContents="lax"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AgedPerson">
<xsd:complexContent mixed="false">
<xsd:extension base="target:Person">
<xsd:choice>
<xsd:element name="age" type="xsd:double" />
<xsd:element name="timeOnEarth" type="xsd:double" />
</xsd:choice>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="don" type="target:Person" />
</xsd:schema>
Figure 4 Schema Object Model Sample
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
class som
{
static readonly XmlQualifiedName xsd_string
= new XmlQualifiedName("string", XmlSchema.Namespace);
static readonly XmlQualifiedName xsd_double
= new XmlQualifiedName("double", XmlSchema.Namespace);
static void Main()
{
string targetNS = "uuid:048b2fa1-d557-473f-ba4c-acee78fe3f7d";
XmlSchema schema = new XmlSchema();
schema.TargetNamespace = targetNS;
// <xsd:element name="name" type="xsd:string" nillable="true" />
XmlSchemaElement elem = new XmlSchemaElement();
elem.Name = "name";
elem.SchemaTypeName = xsd_string;
elem.IsNillable = true;
// <xsd:element name="id" type="xsd:string" />
XmlSchemaElement elem2 = new XmlSchemaElement();
elem2.Name = "id";
elem2.SchemaTypeName = xsd_string;
// <xsd:choice><xsd:element/><xsd:element/><xsd:choice>
XmlSchemaChoice choice = new XmlSchemaChoice();
choice.Items.Add(elem);
choice.Items.Add(elem2);
// <xsd:sequence><xsd:choice/><xsd:any/></xsd:sequence>
XmlSchemaSequence seq = new XmlSchemaSequence();
seq.Items.Add(choice);
seq.Items.Add(new XmlSchemaAny());
// <xsd:complexType><xsd:sequence/></xsd:complexType>
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Particle = seq;
type.Name = "Person";
schema.Items.Add(type);
// <xsd:element name="age" type="xsd:double" />
elem = new XmlSchemaElement();
elem.Name = "age";
elem.SchemaTypeName = xsd_double;
// <xsd:element name="timeOnEarth" type="xsd:double" />
elem2 = new XmlSchemaElement();
elem2.Name = "timeOnEarth";
elem2.SchemaTypeName = xsd_double;
// <xsd:choice><xsd:element/><xsd:element/><xsd:choice>
choice = new XmlSchemaChoice();
choice.Items.Add(elem);
choice.Items.Add(elem);
// <xsd:extension><xsd:choice/></xsd:extension>
XmlSchemaComplexContentExtension content = new
XmlSchemaComplexContentExtension();
content.Particle = choice;
content.BaseTypeName = new XmlQualifiedName("Person", targetNS);
// <xsd:complexContent><xsd:extension/></xsd:complexContent>
XmlSchemaComplexContent model = new XmlSchemaComplexContent();
model.Content = content;
// <xsd:complexType><xsd:complexContent/></xsd:complexType>
type = new XmlSchemaComplexType();
type.ContentModel = model;
type.Name = "AgedPerson";
schema.Items.Add(type);
// <xsd:element />
elem = new XmlSchemaElement();
elem.Name = "don";
elem.SchemaTypeName = new XmlQualifiedName("Person", targetNS);
schema.Items.Add(elem);
// compile and spit out to stdout
schema.Compile(null);
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
schema.Write(writer);
}
}
Figure 7 XMLSerializer Architecture
Figure 8 XmlSerializer (excerpt)
namespace System.Xml.Serialization {
public class XmlSerializer {
// primary constructor
public XmlSerializer(Type clrType);
// primary serialize/deserialize methods
public void Serialize(XmlWriter writer, object obj);
public object Deserialize(XmlReader reader);
// overloaded serialize/deserialize wrappers
public void Serialize(TextWriter writer, object obj);
public object Deserialize(TextReader reader);
public void Serialize(Stream writer, object obj);
public object Deserialize(Stream reader);
}
}
Figure 9 XmlSerializer.Serialize Example
using System.Xml;
using System.Xml.Serialization;
using System.Text;
class MyWriter {
static void
WriteObjectToFile(Object obj, string fname) {
// open an XML output stream
XmlWriter writer = new XmlTextWriter(fname,
Encoding.UTF8);
try {
// create a serializer of the object's type
XmlSerializer ser = new XmlSerializer(obj.GetType());
// write the object out to the XML stream
ser.Serialize(writer, obj);
}
finally {
writer.Close();
}
}
}
Figure 10 XmlSerializer.Deserialize Example
using System.Text;
using System.Xml;
using System.Xml.Serialization
class MyReader {
static object
ReadObjectFromFile(Type type, string fname) {
Object result = null;
// open an XML parser
XmlReader reader = new XmlTextReader(fname);
try {
// create a serializer for the type
XmlSerializer ser = new XmlSerializer(type);
// read XML and create corresponding object
result = ser.Deserialize(reader);
}
finally {
reader.Close();
}
}
return result;
}
Figure 13 [XmlRoot]/[XmlType] Parameters
Use
|
Parameter
|
Type
|
Description
|
R
|
ElementName
|
String
|
xsd:element/@name
|
T
|
TypeName
|
String
|
xsd:complexType/@name
|
R/T
|
Namespace
|
String
|
../xsd:schema/@targetNamespace
|
R
|
IsNullable
|
Boolean
|
xsd:element/@nillable
|
R
|
DataType
|
String
|
XSD Built-in Type Name
|
T
|
IncludeInSchema
|
Boolean
|
No corresponding XML type
|
Figure 14 [XmlRoot]/[XmlType] Example Output
<xsd:schema
xmlns:xsd="https://www.w3.org/2001/XMLSchema"
xmlns:target="abcdef"
targetNamespace="abcdef"
>
<xsd:complexType name="Robert" />
<xsd:element name="Bobby" type="target:Robert"
nillable="true" />
<xsd:complexType name="Steve" />
<xsd:element name="Stevie" type="target:Steve"
nillable="true" />
<xsd:complexType name="Donald" />
</xsd:schema>
<xsd:schema
xmlns:xsd="https://www.w3.org/2001/XMLSchema"
xmlns:foreign="abcdef"
targetNamespace=""
>
<xsd:import namespace="abcdef" />
<xsd:element name="Don" type="foreign:Donald"
nillable="true"/>
<xsd:complexType name="Dave" />
<xsd:element name="Dave" type="Dave"
nillable="true" />
</xsd:schema>
Figure 16 XmlAttributeAttribute/XmlElementAttribute Parameters
Use
|
Parameter
|
Type
|
Description
|
E
|
ElementName
|
String
|
xsd:element/@name
|
A
|
AttributeName
|
String
|
xsd:attribute/@name
|
E/A
|
Form
|
XmlSchemaForm
|
@form
|
E/A
|
Namespace
|
String
|
@ref (combined with XXXName)
|
E/A
|
DataType
|
String
|
XSD Built-in Type Name
|
E
|
IsNullable
|
Boolean
|
xsd:element/@nillable
|
E
|
Type
|
Type
|
Explicitly allowed derived types
|
Figure 19 [XmlAttribute]/[XmlElement] Example
using System.Xml.Schema;
using System.Xml.Serialization;
[ XmlRoot(Name="writer", Namespace="abcdef") ]
[ XmlType(TypeName="Author", Namespace="abcdef") ]
public class Author {
[ XmlElementAttribute("name", IsNullable=true,
Form=XmlSchemaForm.Qualified) ]
public string name;
[ XmlElementAttribute("notbreathing",
Form=XmlForm.Unqualified) ]
public bool dead;
[ XmlAttributeAttribute("rate") ]
public double royaltyrate;
}
Figure 20 [XmlAttribute]/[XmlElement] Example Output
<xsd:schema
xmlns:xsd="https://www.w3.org/2001/XMLSchema"
xmlns:target="abcdef"
targetNamespace="abcdef"
>
<xsd:element name="writer" type="target:Author" />
<xsd:complexType name="Author" >
<xsd:sequence>
<xsd:element name="name" type="xsd:string"
nillable="true"
form="qualified" />
<xsd:element name="notbreathing"
type="xsd:boolean"
/>
</xsd:sequence>
<xsd:attribute name="rate" type="xsd:double" />
</xsd:complexType>
</xsd:schema>
Figure 21 [XmlEnum] Example Output
<xsd:schema
xmlns:xsd="https://www.w3.org/2001/XMLSchema"
xmlns:target="abcdef"
targetNamespace="abcdef"
>
<xsd:simpleType name="hair" >
<xsd:restriction base="xsd:string" >
<xsd:enumeration value="bald" />
<xsd:enumeration value="short" />
<xsd:enumeration value="medium" />
<xsd:enumeration value="long" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Figure 22 [XmlArrayItem] Example
using System.Xml.Schema;
using System.Xml.Serialization;
[ XmlType("company", Namespace="abcdef") ]
public class Company {
[ XmlArray("staff",
Form=XmlSchemaForm.Unqualified) ]
[ XmlArrayItem("manager",
typeof(Manager),
Form=XmlSchemaForm.Unqualified) ]
[ XmlArrayItem("clerk",
typeof(Clerk),
Form=XmlForm.Unqualified) ]
public IEmployee[] Employees;
}
Figure 23 [XmlArrayItem] Example Output
<xsd:complexType name="Company" >
<xsd:sequence>
<xsd:element name="staff">
<xsd:complexType>
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="clerk" type="target:Clerk" />
<xsd:element name="manager" type="target:Manager" />
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
Figure 24 UnknownAttribute Handler
using System.Xml.Serialization
public static void OnAttribute(Object sender,
XmlAttributeEventArgs args) {
string localName = args.Attr.LocalName;
string nsuri = args.Attr.NamespaceURI;
string msg = String.Format(
"Unrecognized attribute {1}:{0} (line {2})",
localName, nsuri, args.LineNumber);
throw new Exception(msg);
}
public static object ReadObject(XmlSerializer ser,
XmlReader reader) {
ser.UnknownAttribute +=
new XmlAttributeEventHandler(OnAttribute);
return ser.Deserialize(reader);
}
|