Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web 310 XML Schema : What You Need to Know and Why Yasser Shohoud Program Manager XML Messaging Microsoft Corporation.

Similar presentations


Presentation on theme: "Web 310 XML Schema : What You Need to Know and Why Yasser Shohoud Program Manager XML Messaging Microsoft Corporation."— Presentation transcript:

1 Web 310 XML Schema : What You Need to Know and Why Yasser Shohoud Program Manager XML Messaging Microsoft Corporation

2 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

3 XML Schema and WSDL Why Care About XML Schema? WSDL describes Web Service end points Specifies operations performed by an endpoint Specifies messages understood by the end point Specifies message types using XML Schema

4 What is XML Schema? Basis of strongly typed XML XML Serialization – ASP.NET Web Services DataSet SQLXML XQuery Contract between producer and consumer of XML

5 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

6 ASP.NET Web Methods Under the Covers Strongly Typed XML Mapped to Objects XML Schema is basis of mapping

7 Validating with XML Schema Use XmlValidatingReader Add the XML schema to its schemas collection Validates as you read Upon error Abort validation Can continue validation if given a ValidationEventHandler Example …

8 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

9 Schema Basics XML Schema lets you declare elements These elements appear in XML documents (instance documents) Each element has a type Choose from over 40 built-in types Or define your own types Defining your own types Simple types: Does not contain elements or attributes Complex types: Contains elements and/or attributes

10 Example Creating an Order schema Creating classes from schema Creating schema from classes

11 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

12 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

13 Enforcing the Contract Having 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

14 XML Schema Best Practices Favor the basic simple types Basic types xs:string xs:int / xs:integer / xs:double / xs:float xs:date / xs:time Esoteric types xs:ENTITIES / xs:NOTATION xs:gMonth, xs:gMonthDay xs:unsignedByte

15 XML Schema Best Practices Use simple type restriction Facets enable stronger contracts Enforce input size (xs:length) Enforce input value range (xs:minInclusive, etc) Enforce input contents (xs:pattern) Restrictions to avoid xs:pattern on non-string types Examples …

16 XML Schema Best Practices elementFormDefault=“qualified” John Smith John Smith <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="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> <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="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" />

17 Without elementFormDefault John Smith John Smith <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="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> <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="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" />

18 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}}

19 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

20 XML Schema Best Practices Extensibility and Open Content Use wildcards to enable extensibility in your messages xs:any xs:anyAttribute Typical gotchas with wildcards ##other processContents = “lax” Non-deterministic schema This is key for extensibility and versioning!

21 XML Schema Best Practices Identity Constraints Prefer key/keyref/unique to id/idref Drawbacks of id/idref Works only on string types Not scoped Limitations on values

22 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

23 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

24 Resources Online resources microsoft.public.dotnet.xml microsoft.public.dotnet.framework.webservic es http://www.msdn.com/columns/xml.asp Real World XML Web Services Addison Wesley, 2003

25 Ask The Experts Get Your Questions Answered Web Services Booth ATE booth 19/20 Tuesday 5pm – 6pm Wednesday 11am – 12 noon

26 Community Resources http://www.microsoft.com/communities/default.mspx Most Valuable Professional (MVP) http://www.mvp.support.microsoft.com/ Newsgroups Converse online with Microsoft Newsgroups, including Worldwide http://www.microsoft.com/communities/newsgroups/default.mspx User Groups Meet and learn with your peers http://www.microsoft.com/communities/usergroups/default.mspx

27 Appendix… Information on using XmlElement parameters and XmlValidatingReader available at http://msdn.microsoft.com/library/en- us/dnservice/html/service04162003.asp 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/frlrfSystemWebServicesProtocolsSoapExt ensionClassTopic.htm http://msdn.microsoft.com/library/en- us/cpref/html/frlrfSystemWebServicesProtocolsSoapExt ensionClassTopic.htm

28 evaluations evaluations

29 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.


Download ppt "Web 310 XML Schema : What You Need to Know and Why Yasser Shohoud Program Manager XML Messaging Microsoft Corporation."

Similar presentations


Ads by Google