web 310 xml schema : what you need to know and why yasser shohoud program manager xml messaging...
TRANSCRIPT
Web 310XML Schema : What You Need to Know and Why
Yasser ShohoudProgram ManagerXML MessagingMicrosoft Corporation
Agenda
Understand what XML Schema is and how it is used by ASMX Web services
Learn to create XML Schemas using Visual Studio
Understand some XML Schema gotchas and best practices
XML Schema and WSDLWhy Care About XML Schema?
WSDL describes Web Service end pointsSpecifies operations performed by an endpoint
Specifies messages understood by the end point
Specifies message types using XML Schema
What is XML Schema?
Basis of strongly typed XMLXML Serialization – ASP.NET Web Services
DataSet
SQLXML
XQuery
Contract between producer and consumer of XML
ASP.NET Web Methods
Regular methods with [WebMethod] attribute
Parameters are CLR objects Return types are CLR objects
[WebMethod] public int Add (int x, int y) { return x + y; }
Transmitted and received on the wire as XML
ASP.NET Web MethodsUnder the Covers
Strongly Typed XML Mapped to ObjectsXML Schema is basis of mapping
Validating with XML Schema
Use XmlValidatingReaderAdd the XML schema to its schemas collection
Validates as you read
Upon errorAbort validation
Can continue validation if given a ValidationEventHandler
Example …
Agenda
Understand what XML Schema is and how it is used by ASMX Web services
Learn to create XML Schemas using Visual Studio
Understand some XML Schema gotchas and best practices
Schema Basics
XML Schema lets you declare elementsThese elements appear in XML documents (instance documents)
Each element has a typeChoose from over 40 built-in types
Or define your own types
Defining your own typesSimple types: Does not contain elements or attributes
Complex types: Contains elements and/or attributes
Agenda
Understand what XML Schema is and how it is used by ASMX Web services
Learn to create XML Schemas using Visual Studio
Understand some XML Schema gotchas and best practices
Beware: Contract not enforced
Message Contract Described by XML Schema
XmlSerializer does not Enforce the Contract
Sequences not enforced
Ignores unexpected elements
Inserts defaults for missing elements
Identity constraints not enforced
Simple type restrictions not checked
Enforcing the ContractHaving your cake and eating it
Use XmlElement parameters and XmlValidatingReader
Use a SoapExtension that uses XmlValidatingReader at BeforeDeserialize stage
But you need to know the best practices
XML Schema Best Practices Favor the basic simple types
Basic typesxs:string
xs:int / xs:integer / xs:double / xs:float
xs:date / xs:time
Esoteric typesxs:ENTITIES / xs:NOTATION
xs:gMonth, xs:gMonthDay
xs:unsignedByte
XML Schema Best Practices Use simple type restriction
Facets enable stronger contractsEnforce input size (xs:length)
Enforce input value range (xs:minInclusive, etc)
Enforce input contents (xs:pattern)
Restrictions to avoidxs:pattern on non-string types
Examples …
XML Schema Best Practices elementFormDefault=“qualified”
<!-- using a schema with elementFormDefault=‘qualified’ makes all elements qualified (not just top-level ones) --><n:Person xmlns:n="http://demos.teched.com/schemas"> <n:FirstName>John</n:FirstName> <n:LastName>Smith</n:LastName></n:Person>
<!-- using a schema with elementFormDefault=‘qualified’ makes all elements qualified (not just top-level ones) --><n:Person xmlns:n="http://demos.teched.com/schemas"> <n:FirstName>John</n:FirstName> <n:LastName>Smith</n:LastName></n:Person>
<xs:schema id="SchemaEx1" elementFormDefault="qualified"targetNamespace="http://demos.teched.com/schemas" xmlns="http://demos.teched.com/schemas" xmlns:mstns="http://demos.teched.com/schemas" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
<xs:schema id="SchemaEx1" elementFormDefault="qualified"targetNamespace="http://demos.teched.com/schemas" xmlns="http://demos.teched.com/schemas" xmlns:mstns="http://demos.teched.com/schemas" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
Without elementFormDefault
<n:Person xmlns:n="http://demos.teched.com/schemas"> <FirstName>John</FirstName> <LastName>Smith</LastName></n:Person>
<n:Person xmlns:n="http://demos.teched.com/schemas"> <FirstName>John</FirstName> <LastName>Smith</LastName></n:Person>
<xs:schema id="SchemaEx1" targetNamespace="http://demos.teched.com/schemas" xmlns="http://demos.teched.com/schemas" xmlns:mstns="http://demos.teched.com/schemas" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
<xs:schema id="SchemaEx1" targetNamespace="http://demos.teched.com/schemas" xmlns="http://demos.teched.com/schemas" xmlns:mstns="http://demos.teched.com/schemas" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
XML Schema Best Practices Global elements over global types
Global schema components can be reused by other schemas
Model groups and complex types are content models
Prefer exposing named elements and attributes
{ element ns:name {xs:string} } of type P
ns:person { element ns:name {xs:string}}
XML Schema Best Practices Groups vs. Complex types
What is a complex type?xs:group + xs:attributeGroup + type derivation
Use groups if content model not reused
Groups are externally visible, anonymous complex types are not
XML Schema Best Practices Extensibility and Open Content
Use wildcards to enable extensibility in your messages
xs:anyxs:anyAttribute
Typical gotchas with wildcards ##other processContents = “lax” Non-deterministic schema
This is key for extensibility and versioning!
XML Schema Best Practices Identity Constraints
Prefer key/keyref/unique to id/idref
Drawbacks of id/idrefWorks only on string types
Not scoped
Limitations on values
XML Schema Best Practices Versioning Schemas
Message formats should have a version attribute
Incremental, backwards compatible changes
Increase version number
Significant, backwards incompatible changes
Change namespace name
Summary
XML Schemas are used to define your data structures
Document and message format
The basis of Web services contracts
You can use Visual Studio to design your schemas
Whatever tool you use, apply best practices
Open content model is key for versioning
Keep it simple for interop
ResourcesResourcesOnline resources
microsoft.public.dotnet.xml
microsoft.public.dotnet.framework.webservices
http://www.msdn.com/columns/xml.asp
Real World XML Web ServicesAddison Wesley, 2003
Ask The ExpertsGet Your Questions Answered
Web Services BoothATE booth 19/20
Tuesday 5pm – 6pm
Wednesday 11am – 12 noon
Community Resources
Community Resourceshttp://www.microsoft.com/communities/default.mspx
Most Valuable Professional (MVP)http://www.mvp.support.microsoft.com/
NewsgroupsConverse online with Microsoft Newsgroups, including Worldwidehttp://www.microsoft.com/communities/newsgroups/default.mspx
User GroupsMeet and learn with your peershttp://www.microsoft.com/communities/usergroups/default.mspx
Appendix…Appendix…• Information on using XmlElement
parameters and XmlValidatingReader available at http://msdn.microsoft.com/library/en-us/dnservice/html/service04162003.asp
• Information on SoapExtension class at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebServicesProtocolsSoapExtensionClassTopic.htm
© 2003 Microsoft Corporation. All rights reserved.© 2003 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.