Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create .Net classes from xml ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 168
    Comment on it

    There are few steps to convert XML to .Net classes. These are:

    1. Copy your XML content from any file.
    2. In Visual studio, open the file where you have to create classes in .Net 
    3. Place the cursor in that position, Go to menu bar->select EDIT-> Select Paste Special->Select Paste XML as Classes.

     

     

     

    Example:

    We have a below XML:

     

    <?xml version="1.0"?>
    
     <EmployeeDetails>
      <FirstName>Mona</FirstName>
     <LastName>Jain</LastName>
      <Age>25</Age>
      <EmailId>test@test.com</EmailId>
      <City>Delhi</City>
      <Country>India</Country>
     </EmployeeDetails>


      
    To Convert this XML into .net classes follow these steps:
    1. Copy this XML
    2. Paste this XML in Visual Studio file as "Paste XML as Classes" then you get "EmployeeDetails" class automatically :

    Here, below is the code that will generated automatically .

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class EmployeeDetails
    {
    
    	private string firstNameField;
    
    	private string lastNameField;
    
    	private byte ageField;
    
    	private string emailIdField;
    
    	private string cityField;
    
    	private string countryField;
    
    	/// <remarks/>
    	public string FirstName
    	{
    		get
    		{
    			return this.firstNameField;
    		}
    		set
    		{
    			this.firstNameField = value;
    		}
    	}
    
    	/// <remarks/>
    	public string LastName
    	{
    		get
    		{
    			return this.lastNameField;
    		}
    		set
    		{
    			this.lastNameField = value;
    		}
    	}
    
    	/// <remarks/>
    	public byte Age
    	{
    		get
    		{
    			return this.ageField;
    		}
    		set
    		{
    			this.ageField = value;
    		}
    	}
    
    	/// <remarks/>
    	public string EmailId
    	{
    		get
    		{
    			return this.emailIdField;
    		}
    		set
    		{
    			this.emailIdField = value;
    		}
    	}
    
    	/// <remarks/>
    	public string City
    	{
    		get
    		{
    			return this.cityField;
    		}
    		set
    		{
    			this.cityField = value;
    		}
    	}
    
    	/// <remarks/>
    	public string Country
    	{
    		get
    		{
    			return this.countryField;
    		}
    		set
    		{
    			this.countryField = value;
    		}
    	}
    }

     

    This feature is only available in .NET 4.5  framework in VS2012(or newer).

     

    Thanks

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: