Posts

Showing posts with the label XmlSerialzer

Donate

Serialize .NET Classes With Inheritance To XML In C#

Good day! Here's a simple example on how to Serialize .NET classes that applied the concept of Inheritance to XML. Given the model classes below: [Serializable] public class Employee { public int EmployeeId { get ; set ; } public string Name { get ; set ; } public string Address { get ; set ; } } [Serializable] public class Utility : Employee { public string Category { get ; set ; } } [Serializable] public class Supervisor : Employee { public int OverrideCode { get ; set ; } } The code to populate and serialize the objects to XML is presented here using XmlSerializer class: List<Employee> employees = new List<Employee>(); Supervisor supervisor1 = new Supervisor(); supervisor1.Name = "Michael" ; supervisor1.Address = "Manila" ; supervisor1.EmployeeId = 11111; supervisor1.OverrideCode = 234; employees.Add(supervisor1); Utility utility1 = new Utility(); utility1.Name = "Erick" ; utility1.Address = "Masba

Create XmlElement Using XmlSerializer In C#.NET

Image
Given the XML data below, the product properties are generated below the products node. What I want is to structure the product properties inside an element Product while serializing. <?xml version="1.0" encoding="utf-8"?> <Products xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/XMLSchema" > <Code>12345</Code> <Name>Samsung Galaxy</Name> <Model>Galaxy</Model> <Manufacturer>Samsung Ltd</Manufacturer> <OperatingSystem>Jelly Bean</OperatingSystem> <Distributor>Junrex</Distributor> <Version>7.0</Version> </Products> To achieve that, I have modified the model classes by separating the properties into another class and in the Products class I created a property of type List with XML Attribute Product . public class Products { [XmlElement("Product")] publ

Donate