Monday, February 6, 2012

Tech Review Software Development: Friday, February 3, 2012

XML Schema Tutorial

XML Schema
XML Schema

  • XML Schema Definition (XSD)
  • what is an XML Schema?
    • defines elements that can appear
    • defines attributes that can appear
    • defines which elements are child elements
    • defines order of child elements
    • defines number of child elements
    • defines if empty or can include text
    • defines data types for elements and attributes
    • defines default and fixed values for elements and attributes
  • Can reference schema in XML doc
  • XML Schema built-in data types
    • xs:string
    • xs:decimal
    • xs:integer
    • xs:boolean
    • xs:date
    • xs:time
  • if an elements has attributes it is considered complex
  • Can add restrictions or 'facets' to a element or attribute
D:\\test>xsd webSearch.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\\test\\webSearch.xsd'.
D:\\test>

D:\\test>xsd webSearch.xsd /CLASSES /language:vb
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\\test\\webSearch.vb'.
D:\\test>xsd webSearch.xsd /CLASSES
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\\test\\webSearch.cs'.

Path to XML Help files:  C:\Users\Public\Public XPlan AppData\FalconView\config

An example of how to use XML Schema validation:  http://support.microsoft.com/kb/307379

An example of reading and writing using XML in C#:  

XML Tutorial

  • self-describing syntax
  • first line is XML declaration
    • version (1.0)
    • encoding (ISO-8859-1...)
  • root element
  • child elements
<root>
<child>
<subchild>.....</subchild>
</chiuld>
</root>

  • elements
    • <root> <child><subchild>textcontent</subchild>
    • child is an element of root
    • subchild is an element of child
  • text content
    • <root> <child><subchild>textcontent</subchild>
  • attributes
    • <book category="COOKING">
  • must have closing tags.
  • tags are case sensitive
  • opening and closing tags
    • equal to start and end tags
  • must be properly nested
  • must have one root element
  • all attributes must be quoted.
  • entity references
    • &lt; < less than
    • &gt; > greater than
    • &amp; & ampersand 
    • &apos; ' apostrophe
    • &quot; " quotation mark
  • only "<" and "&" are strictly illegal
  • XML declaration
    • <?xml verison.....?>
  • comments in XML
    • <!-- comment -->
  • new lines are store as line feeds (LFs)
  • XML naming rules
    • letters, numbers, and other characters
    • cannot start with number or punctuation
    • cannot start with xml, XML or any combination that spells xml
    • cannot contain spaces
  • Naming practices
How do I read and write XML in C#?

  • attribute
    • provides additional info about element
  • may want to avoid attributes in XML
  • mete data
    • data about data
    • store as attribute
  • data
    • store as elements
  • DTD
    • defines structure of XML
  • XML Schema
    • defines structure of XML
  • In XML errors are not allowed
  • can use CSS (Casading Style Sheets) to display info to an XML document
  • XSLT is recommended to display XML
  • SOAP
    • exchanges XML between applications
XML Definition:

What is the format of the XML files we need to read in?

Nice Opensource XML tool:



No comments: