JG Vimalan's Blog

It is all about coding!

Using xsd.exe

I got a task where I need to read data from an xml file. So, as a first step, I used the xsd.exe, which comes with visual studio for reading the xml data. the details are given below,

Assume that, you have your xml file as shown below,

Now, to create a class for this xml file, I am going to use the xsd.exe tool. Launch visual studio command prompt and type xsd.exe [xml file path] . An xsd file will be created. Now type xsd.exe [xsd file path] /c. This will create the corresponding class file. See the image below for more details,

Now, the generated class file will be as shown below, this class file can be used in your project while serializing/deserializing the xml file.

//——————————————————————————
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4952
//
//     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.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “2.0.50727.3038”)]
[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 Staff {
   
    private string yearField;
   
    private StaffPerson[] personField;
   
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Year {
        get {
            return this.yearField;
        }
        set {
            this.yearField = value;
        }
    }
   
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(“Person”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public StaffPerson[] Person {
        get {
            return this.personField;
        }
        set {
            this.personField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “2.0.50727.3038”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“code”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class StaffPerson {
   
    private string nameField;
   
    private string ageField;
   
    private string genderField;
   
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
   
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Age {
        get {
            return this.ageField;
        }
        set {
            this.ageField = value;
        }
    }
   
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Gender {
        get {
            return this.genderField;
        }
        set {
            this.genderField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “2.0.50727.3038”)]
[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 NewDataSet {
   
    private Staff[] itemsField;
   
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(“Staff”)]
    public Staff[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

Advertisements

December 2, 2010 - Posted by | C#.NET

1 Comment »

  1. Hi,
    I am trying to do something similar but I am stuck at the following line:

    private StaffPerson[] personField;

    How do I make use of this field in the code? I have instantiated the class Staff and I can access the field personField there but I keep getting null pointer exception or some other error.

    Comment by Manish Bansal | December 22, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: