Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Wednesday, June 18, 2008

* Celebrating 10 Years of XML

image

Check out the official celebration pages : http://www.w3.org/2008/xml10/

XML in 10 points

  1. XML is for structuring data
  2. XML looks a bit like HTML
  3. XML is text, but isn't meant to be read
  4. XML is verbose by design
  5. XML is a family of technologies
  6. XML is new, but not that new
  7. XML leads HTML to XHTML
  8. XML is modular
  9. XML is the basis for RDF and the Semantic Web
  10. XML is license-free, platform-independent and well-supported

More details http://www.w3.org/XML/1999/XML-in-10-points

Thursday, June 07, 2007

* Document Markup Languages : Office Open XML vs Open Document

Both Office Open XML (OOXML) and Open Document (OD) are document markup languages which define the storage structure of Office tools and currently both are competing with each other.

Here are few points about their history, similarities, differences and support for them in community.

  • Both are XML based formats for storing office documents.
  • OOXML is defined and supported by Microsoft and became ECMA standard in 2006. OD is supported by SUN and IBM and became ISO standard in 2006.
  • OOXML is a native format for MS Office 2007 and OD is native format for OpenOffice & StarOffice.
  • Both formats are still incomplete and evolving.
  • OOXML uses non-mixed content model while OD uses mixed content model.
  • OOXML stores external references like hyperlinks in a separate XML file while OD keeps them in main file.
  • OOXML uses short tags for XML while OD uses long tags. For large documents this can be a performance issue in case of OD as parsing and compression may take more resources.
  • Currently OD does not support digital signatures.
  • Tools supporting or going to support OOXML natively : MS Office, Corel etc.
  • Tools supporting or going to support OD natively : Corel, Lotus, OpenOffice, StarOffice, Google Docs.
  • Several Open Source plugins available for MS Office to open and save OD documents.
  • Recently Microsoft released the sdk for OOXML based documents.
  • Considering the wide spread use of MS Office and multi vendor support for OD, it seems both the standards are going to stay in future and hopefully we will see some converters for interoperability between the two.

Other Posts

    Friday, April 13, 2007

    * Tool : XML Schema Definition Tool (Xsd.exe)

    XSD.exe which comes as part of .net framework is mainly used to generate XML schemas or .net classes based on XML schema.

    Some of the generations possible with this tool are :

    • XDR -> XSD : generates xml schema based on xml data reduced schema
    • XML -> XSD : generates xml schema based on xml data file
    • XSD -> Dataset : generates code of dataset based class which can be used to read xml data compliant to input xsd using System.Data.DataSet.ReadXml
    • XSD -> Classes : generates code of classes which can be used to read and write xml data using System.Xml.Serialization.XmlSerializer
    • Classes -> XSD : generates xml schema for types in a runtime assembly

     Lets create a sample xml data file and work on it using xsd tool.

    Consider the following xml data file EmployeeList.xml:

    <EmployeeList>
    <Company ID="1">
    <Employee>Employee11</Employee>
    <Employee>Employee12</Employee>
    <Employee>Employee13</Employee>
    </Company>
    <Company ID="2">
    <Employee>Employee21</Employee>
    <Employee>Employee22</Employee>
    <Employee>Employee23</Employee>
    </Company>
    <Company ID="3">
    <Employee>Employee31</Employee>
    <Employee>Employee32</Employee>
    <Employee>Employee33</Employee>
    <Employee>Employee34</Employee>
    </Company>
    </EmployeeList>


    Execute XSD.exe on the xml data file:



    > XSD EmployeeList.xml



    The resultant xsd file EmployeeList.xsd will be :

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="EmployeeList" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="EmployeeList" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="Company">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Employee" nillable="true" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:simpleContent msdata:ColumnName="Employee_Text" msdata:Ordinal="0">
    <xs:extension base="xs:string">
    </xs:extension>
    </xs:simpleContent>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="ID" type="xs:string" />
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>


    Now we can generate code of classes using the same tool and xsd generated which can be used to read and write xml data using System.Xml.Serialization.XmlSerializer.



    >xsd EmployeeList.xsd /classes



    The code generated is :

    //------------------------------------------------------------------------------
    // <auto-generated>
    // This code was generated by a tool.
    // Runtime Version:2.0.50727.42
    //
    // Changes to this file may cause incorrect behavior and will be lost if
    // the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    using System.Xml.Serialization;

    //
    // This source code was auto-generated by xsd, Version=2.0.50727.42.
    //


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class EmployeeList {

    private EmployeeListCompany[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Company", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public EmployeeListCompany[] Items {
    get {
    return this.itemsField;
    }
    set {
    this.itemsField = value;
    }
    }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class EmployeeListCompany {

    private EmployeeListCompanyEmployee[] employeeField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Employee", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public EmployeeListCompanyEmployee[] Employee {
    get {
    return this.employeeField;
    }
    set {
    this.employeeField = value;
    }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ID {
    get {
    return this.idField;
    }
    set {
    this.idField = value;
    }
    }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class EmployeeListCompanyEmployee {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
    get {
    return this.valueField;
    }
    set {
    this.valueField = value;
    }
    }
    }


    The sample usage of this class can be as below:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;

    namespace XSD_TOOL_SAMPLE
    {
    class Program
    {
    static void Main(string[] args)
    {
    EmployeeList empList;
    XmlSerializer xs = new XmlSerializer(typeof(EmployeeList));
    FileStream fs = new FileStream(@"D:\Vikas\vikas\tech\code\CSharp\XML\EmployeeList.xml",FileMode.Open);
    empList = (EmployeeList)xs.Deserialize(fs);
    Console.WriteLine(empList.Items[0].Employee[0].Value);
    }
    }
    }


    The output for this will be the value of first Employee element.



    Output : Employee11



    More detailed documentation on xsd tool can be found on msdn.



    Other Posts



    Monday, April 02, 2007

    * Tool : XML Notepad

    XML Notepad is a UI based XML browsing & editing tool built using .NET framework 2.0.

    It is developed by XML tools team at Microsoft.

    The main features of this tool are :

    • Tree View synchronized with Node Text View for quick editing of node names and values.
    • Real time XML schema validation with errors and warning shown.
    • Intellisense based on schema attached.
    • Mapping of Namespace URIs and file names containing schemas.
    • XML difference tool
    • XSLT transformation output.
    • Drag/Drop & Copy/Paste feature for nodes.

    It can be downloaded from following location.

    ~tata & take care~

    Other Posts

    Thursday, March 29, 2007

    * Reading & Writing XML Data

    This post covers how to read and write XML data using XmlReader and XmlWriter classes provided by .NET framework.

    To get the Overview of XML support in .NET Framework pls read following post.

    Classes XmlReader & XmlWriter provide support for forward-only, read-only & write-only and non-cached way of reading & writing XML data.

    For reading the data, XmlReader provides the support for traversing through node, data, attributes, etc. XmlReaderSettings attached to XmlReader instance controls the behaviour of XmlReader like setting the validation type, setting event handler for validation failure etc. XmlSchemaSet can be used to specify the schema file to be used in case validation is of type Schema.

    For writing XML, XmlWriter provide the support for writing xml data to file in forward-only manner. XmlWriterSettings controls the behaviour of XmlWriter class like Indentation and Indentation characters to be used.

    Below sample demonstrates how to read a XML file and reproduce the same file again by reading each part of xml one by one and writing it to new file. It uses the schema file to validate the xml data.

    The XML used for testing is :

    <EmployeeList>
    <Company ID="1">
    <Employee>Employee11</Employee>
    <Employee>Employee12</Employee>
    <Employee>Employee13</Employee>
    </Company>
    <Company ID="2">
    <Employee>Employee21</Employee>
    <Employee>Employee22</Employee>
    <Employee>Employee23</Employee>
    </Company>
    <Company ID="3">
    <Employee>Employee31</Employee>
    <Employee>Employee32</Employee>
    <Employee>Employee33</Employee>
    <Employee>Employee34</Employee>
    </Company>
    </EmployeeList>


    The schema file generated using XSD.exe tool is :

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="EmployeeList" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="EmployeeList" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="Company">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Employee" nillable="true" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:simpleContent msdata:ColumnName="Employee_Text" msdata:Ordinal="0">
    <xs:extension base="xs:string">
    </xs:extension>
    </xs:simpleContent>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="ID" type="xs:string" />
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>


    The code of the program :

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Xml.Schema;

    namespace XMLReadWrite
    {
    class Program
    {
    static void Main(string[] args)
    {
    try
    {
    XmlSchemaSet schemaSet = new XmlSchemaSet();
    schemaSet.Add(null, "EmployeeList.xsd");

    XmlReaderSettings readerSettings = new XmlReaderSettings();
    readerSettings.CheckCharacters = true;
    readerSettings.ValidationType = ValidationType.Schema;
    readerSettings.Schemas = schemaSet;
    readerSettings.ValidationEventHandler += new ValidationEventHandler(ValCallBack);

    XmlWriterSettings writerSettings = new XmlWriterSettings();
    writerSettings.Indent = true;
    writerSettings.IndentChars = "\t";

    XmlReader reader = XmlReader.Create("EmployeeList.xml", readerSettings);

    XmlWriter writer = XmlWriter.Create("EmployeeListNew.xml", writerSettings);

    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element:
    Console.WriteLine("element <{0}>", reader.Name);
    writer.WriteStartElement(reader.Name);
    break;
    case XmlNodeType.Text:
    Console.WriteLine("text "+reader.Value);
    writer.WriteString(reader.Value);
    break;
    case XmlNodeType.CDATA:
    Console.WriteLine("CDATA "+reader.Value);
    break;
    case XmlNodeType.ProcessingInstruction:
    Console.WriteLine("<?{0} {1}?>", reader.Name, reader.Value);
    break;
    case XmlNodeType.Comment:
    Console.WriteLine("<!--{0}-->", reader.Value);
    break;
    case XmlNodeType.XmlDeclaration:
    Console.WriteLine("<?xml version='1.0'?>");
    break;
    case XmlNodeType.Document:
    break;
    case XmlNodeType.DocumentType:
    Console.WriteLine("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
    break;
    case XmlNodeType.EntityReference:
    Console.Write(reader.Name);
    break;
    case XmlNodeType.EndElement:
    Console.Write("end element </{0}>", reader.Name);
    writer.WriteEndElement();
    break;
    }
    if (reader.HasAttributes)
    {
    Console.WriteLine("Attributes of <" + reader.Name + ">");
    while (reader.MoveToNextAttribute())
    {
    Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
    writer.WriteAttributeString(reader.Name, reader.Value);
    }
    }
    }
    writer.Flush();
    }
    catch (System.Exception ex)
    {
    Console.WriteLine(ex.Message);
    Console.WriteLine(ex.StackTrace);
    }
    }
    private static void ValCallBack(object sender, ValidationEventArgs e)
    {
    Console.WriteLine("Validation Errors: {0}", e.Message);
    }
    }
    }


    ~tata & take care~



    Other Posts



    Thursday, March 22, 2007

    * Overview of XML support in .NET framework

    MS.NET framework provides an exhaustive set of classes to work on XML data and documents. Some of the main operations it supports are :

    • Parsing & writing XML
    • Manipulating in-memory XML
    • XML data validation
    • XSLT transformation

    The base namespace where all the XML related classes are placed is System.Xml.

    Some of the main classes available are :

    XmlReader : Stream based non-cached forward only reading of XML data.

    XmlWriter : Stream based non-cached forward only writing of XML data.

    XPathNavigator : In-Memory processing ( editing & navigation ) of data stored in XPathDocument or XmlDocument

    XmlDocument : Storing XML data & based on W3C Document Object Model (DOM)

    XslCompiledTransform : XSLT processor

    XmlResolver : Used to resolve external resources in XML documents

    Will cover the detailed usage of above classes in next few posts on this.

    ~tata & take care~

    Other Posts