none
How to serialize/ deserialize master detail xml to class RRS feed

  • Question

  • suppose below master detail xml is stored in string variable. now how to write class as a result i can deserialize the below xml to my class and also serialize my class to xml which will be stored in string variable. looking for example code.

    when to use xml array attribute ?

    <Root>
      <Orders>
        <OrderID>10248</OrderID>
        <CustomerID>VINET</CustomerID>
        <EmployeeID>5</EmployeeID>
        <OrderDate>1996-07-04T00:00:00</OrderDate>
        <RequiredDate>1996-08-01T00:00:00</RequiredDate>
        <ShippedDate>1996-07-16T00:00:00</ShippedDate>
        <ShipVia>3</ShipVia>
        <Freight>32.3800</Freight>
        <ShipName>Vins et alcools Chevalier</ShipName>
        <ShipAddress>59 rue de l'Abbaye</ShipAddress>
        <ShipCity>Reims</ShipCity>
        <ShipPostalCode>51100</ShipPostalCode>
        <ShipCountry>France</ShipCountry>
        <OrderDetails>
          <OrderID>10248</OrderID>
          <ProductID>11</ProductID>
          <UnitPrice>14.0000</UnitPrice>
          <Quantity>12</Quantity>
          <Discount>0.0000000e+000</Discount>
        </OrderDetails>
        <OrderDetails>
          <OrderID>10248</OrderID>
          <ProductID>42</ProductID>
          <UnitPrice>9.8000</UnitPrice>
          <Quantity>10</Quantity>
          <Discount>0.0000000e+000</Discount>
        </OrderDetails>
        <OrderDetails>
          <OrderID>10248</OrderID>
          <ProductID>72</ProductID>
          <UnitPrice>34.8000</UnitPrice>
          <Quantity>5</Quantity>
          <Discount>0.0000000e+000</Discount>
        </OrderDetails>
      </Orders>
      <Orders>
        <OrderID>10249</OrderID>
        <CustomerID>TOMSP</CustomerID>
        <EmployeeID>6</EmployeeID>
        <OrderDate>1996-07-05T00:00:00</OrderDate>
        <RequiredDate>1996-08-16T00:00:00</RequiredDate>
        <ShippedDate>1996-07-10T00:00:00</ShippedDate>
        <ShipVia>1</ShipVia>
        <Freight>11.6100</Freight>
        <ShipName>Toms Spezialitäten</ShipName>
        <ShipAddress>Luisenstr. 48</ShipAddress>
        <ShipCity>Münster</ShipCity>
        <ShipPostalCode>44087</ShipPostalCode>
        <ShipCountry>Germany</ShipCountry>
        <OrderDetails>
          <OrderID>10249</OrderID>
          <ProductID>14</ProductID>
          <UnitPrice>18.6000</UnitPrice>
          <Quantity>9</Quantity>
          <Discount>0.0000000e+000</Discount>
        </OrderDetails>
        <OrderDetails>
          <OrderID>10249</OrderID>
          <ProductID>51</ProductID>
          <UnitPrice>42.4000</UnitPrice>
          <Quantity>40</Quantity>
          <Discount>0.0000000e+000</Discount>
        </OrderDetails>
      </Orders>
      <Orders>
        <OrderID>10250</OrderID>
        <CustomerID>HANAR</CustomerID>
        <EmployeeID>4</EmployeeID>
        <OrderDate>1996-07-08T00:00:00</OrderDate>
        <RequiredDate>1996-08-05T00:00:00</RequiredDate>
        <ShippedDate>1996-07-12T00:00:00</ShippedDate>
        <ShipVia>2</ShipVia>
        <Freight>65.8300</Freight>
        <ShipName>Hanari Carnes</ShipName>
        <ShipAddress>Rua do Paço, 67</ShipAddress>
        <ShipCity>Rio de Janeiro</ShipCity>
        <ShipRegion>RJ</ShipRegion>
        <ShipPostalCode>05454-876</ShipPostalCode>
        <ShipCountry>Brazil</ShipCountry>
        <OrderDetails>
          <OrderID>10250</OrderID>
          <ProductID>41</ProductID>
          <UnitPrice>7.7000</UnitPrice>
          <Quantity>10</Quantity>
          <Discount>0.0000000e+000</Discount>
        </OrderDetails>
        <OrderDetails>
          <OrderID>10250</OrderID>
          <ProductID>51</ProductID>
          <UnitPrice>42.4000</UnitPrice>
          <Quantity>35</Quantity>
          <Discount>1.5000001e-001</Discount>
        </OrderDetails>
        <OrderDetails>
          <OrderID>10250</OrderID>
          <ProductID>65</ProductID>
          <UnitPrice>16.8000</UnitPrice>
          <Quantity>15</Quantity>
          <Discount>1.5000001e-001</Discount>
        </OrderDetails>
      </Orders>
    </Root>


    • Edited by Sudip_inn Wednesday, August 15, 2018 6:14 PM
    Wednesday, August 15, 2018 6:12 PM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, if you wanna the classes of the xml which is stored in string variable, you could try the way below.

    Copy the xml data> Click Edit in Visual Studio> Choose Paste Special> Paste XML As Classes

    After that you could get the classes you want.

    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Root
        {
    
            private RootOrders[] ordersField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Orders")]
            public RootOrders[] Orders
            {
                get
                {
                    return this.ordersField;
                }
                set
                {
                    this.ordersField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrders
        {
    
            private ushort orderIDField;
    
            private string customerIDField;
    
            private byte employeeIDField;
    
            private System.DateTime orderDateField;
    
            private System.DateTime requiredDateField;
    
            private System.DateTime shippedDateField;
    
            private byte shipViaField;
    
            private decimal freightField;
    
            private string shipNameField;
    
            private string shipAddressField;
    
            private string shipCityField;
    
            private string shipRegionField;
    
            private string shipPostalCodeField;
    
            private string shipCountryField;
    
            private RootOrdersOrderDetails[] orderDetailsField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public string CustomerID
            {
                get
                {
                    return this.customerIDField;
                }
                set
                {
                    this.customerIDField = value;
                }
            }
    
            /// <remarks/>
            public byte EmployeeID
            {
                get
                {
                    return this.employeeIDField;
                }
                set
                {
                    this.employeeIDField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime OrderDate
            {
                get
                {
                    return this.orderDateField;
                }
                set
                {
                    this.orderDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime RequiredDate
            {
                get
                {
                    return this.requiredDateField;
                }
                set
                {
                    this.requiredDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime ShippedDate
            {
                get
                {
                    return this.shippedDateField;
                }
                set
                {
                    this.shippedDateField = value;
                }
            }
    
            /// <remarks/>
            public byte ShipVia
            {
                get
                {
                    return this.shipViaField;
                }
                set
                {
                    this.shipViaField = value;
                }
            }
    
            /// <remarks/>
            public decimal Freight
            {
                get
                {
                    return this.freightField;
                }
                set
                {
                    this.freightField = value;
                }
            }
    
            /// <remarks/>
            public string ShipName
            {
                get
                {
                    return this.shipNameField;
                }
                set
                {
                    this.shipNameField = value;
                }
            }
    
            /// <remarks/>
            public string ShipAddress
            {
                get
                {
                    return this.shipAddressField;
                }
                set
                {
                    this.shipAddressField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCity
            {
                get
                {
                    return this.shipCityField;
                }
                set
                {
                    this.shipCityField = value;
                }
            }
    
            /// <remarks/>
            public string ShipRegion
            {
                get
                {
                    return this.shipRegionField;
                }
                set
                {
                    this.shipRegionField = value;
                }
            }
    
            /// <remarks/>
            public string ShipPostalCode
            {
                get
                {
                    return this.shipPostalCodeField;
                }
                set
                {
                    this.shipPostalCodeField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCountry
            {
                get
                {
                    return this.shipCountryField;
                }
                set
                {
                    this.shipCountryField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("OrderDetails")]
            public RootOrdersOrderDetails[] OrderDetails
            {
                get
                {
                    return this.orderDetailsField;
                }
                set
                {
                    this.orderDetailsField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrdersOrderDetails
        {
    
            private ushort orderIDField;
    
            private byte productIDField;
    
            private decimal unitPriceField;
    
            private byte quantityField;
    
            private double discountField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public byte ProductID
            {
                get
                {
                    return this.productIDField;
                }
                set
                {
                    this.productIDField = value;
                }
            }
    
            /// <remarks/>
            public decimal UnitPrice
            {
                get
                {
                    return this.unitPriceField;
                }
                set
                {
                    this.unitPriceField = value;
                }
            }
    
            /// <remarks/>
            public byte Quantity
            {
                get
                {
                    return this.quantityField;
                }
                set
                {
                    this.quantityField = value;
                }
            }
    
            /// <remarks/>
            public double Discount
            {
                get
                {
                    return this.discountField;
                }
                set
                {
                    this.discountField = value;
                }
            }
        }
    
    

    There are a lot of data in your xml string.

    If you want to deserialize xml string, I use another xml string make a simple code for your reference.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApp
    {
        class Deserialization_XML_string
        {
            static void Main(string[] args)
            {
                // Prepare XML string
                string xmlString = "<Products><Product><Id>1</Id><Name>My XML product</Name></Product><Product><Id>2</Id><Name>My second product</Name></Product></Products>";
                //Create XML serializer
                XmlSerializer serializer = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("Products"));
                //Create StringReader object
                StringReader stringReader = new StringReader(xmlString);
                //deserialize to your C# object
                List<Product> productList = (List<Product>)serializer.Deserialize(stringReader);
            }
    
        }
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
    

    According to your xml data, I suggest you to read xml from xml file. Here is a simple example for your reference.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApp
    {
        class Deserialization_XML_strings2
        {
            static void Main(string[] args)
            {
                string filename = @"C:\Users\wendyz.WICRESOFT.000\OneDrive\Projects\VS2017 Project\ConsoleApp\ConsoleApp\Deserialization XML strings2.xml";
                // Create an instance of the XmlSerializer specifying type and namespace.
                XmlSerializer serializer = new
                XmlSerializer(typeof(Root));
    
                // A FileStream is needed to read the XML document.
                FileStream fs = new FileStream(filename, FileMode.Open);
                XmlReader reader = XmlReader.Create(fs);
    
                // Declare an object variable of the type to be deserialized.
                Root R;
    
                // Use the Deserialize method to restore the object's state.
                R = (Root)serializer.Deserialize(reader);
                fs.Close();
                foreach (var item in R.Orders)
                {
                    Console.WriteLine(item.CustomerID);
                }
                
                Console.ReadKey();
    
            }
        }
    
        // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Root
        {
    
            private RootOrders[] ordersField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Orders")]
            public RootOrders[] Orders
            {
                get
                {
                    return this.ordersField;
                }
                set
                {
                    this.ordersField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrders
        {
    
            private ushort orderIDField;
    
            private string customerIDField;
    
            private byte employeeIDField;
    
            private System.DateTime orderDateField;
    
            private System.DateTime requiredDateField;
    
            private System.DateTime shippedDateField;
    
            private byte shipViaField;
    
            private decimal freightField;
    
            private string shipNameField;
    
            private string shipAddressField;
    
            private string shipCityField;
    
            private string shipRegionField;
    
            private string shipPostalCodeField;
    
            private string shipCountryField;
    
            private RootOrdersOrderDetails[] orderDetailsField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public string CustomerID
            {
                get
                {
                    return this.customerIDField;
                }
                set
                {
                    this.customerIDField = value;
                }
            }
    
            /// <remarks/>
            public byte EmployeeID
            {
                get
                {
                    return this.employeeIDField;
                }
                set
                {
                    this.employeeIDField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime OrderDate
            {
                get
                {
                    return this.orderDateField;
                }
                set
                {
                    this.orderDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime RequiredDate
            {
                get
                {
                    return this.requiredDateField;
                }
                set
                {
                    this.requiredDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime ShippedDate
            {
                get
                {
                    return this.shippedDateField;
                }
                set
                {
                    this.shippedDateField = value;
                }
            }
    
            /// <remarks/>
            public byte ShipVia
            {
                get
                {
                    return this.shipViaField;
                }
                set
                {
                    this.shipViaField = value;
                }
            }
    
            /// <remarks/>
            public decimal Freight
            {
                get
                {
                    return this.freightField;
                }
                set
                {
                    this.freightField = value;
                }
            }
    
            /// <remarks/>
            public string ShipName
            {
                get
                {
                    return this.shipNameField;
                }
                set
                {
                    this.shipNameField = value;
                }
            }
    
            /// <remarks/>
            public string ShipAddress
            {
                get
                {
                    return this.shipAddressField;
                }
                set
                {
                    this.shipAddressField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCity
            {
                get
                {
                    return this.shipCityField;
                }
                set
                {
                    this.shipCityField = value;
                }
            }
    
            /// <remarks/>
            public string ShipRegion
            {
                get
                {
                    return this.shipRegionField;
                }
                set
                {
                    this.shipRegionField = value;
                }
            }
    
            /// <remarks/>
            public string ShipPostalCode
            {
                get
                {
                    return this.shipPostalCodeField;
                }
                set
                {
                    this.shipPostalCodeField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCountry
            {
                get
                {
                    return this.shipCountryField;
                }
                set
                {
                    this.shipCountryField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("OrderDetails")]
            public RootOrdersOrderDetails[] OrderDetails
            {
                get
                {
                    return this.orderDetailsField;
                }
                set
                {
                    this.orderDetailsField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrdersOrderDetails
        {
    
            private ushort orderIDField;
    
            private byte productIDField;
    
            private decimal unitPriceField;
    
            private byte quantityField;
    
            private double discountField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public byte ProductID
            {
                get
                {
                    return this.productIDField;
                }
                set
                {
                    this.productIDField = value;
                }
            }
    
            /// <remarks/>
            public decimal UnitPrice
            {
                get
                {
                    return this.unitPriceField;
                }
                set
                {
                    this.unitPriceField = value;
                }
            }
    
            /// <remarks/>
            public byte Quantity
            {
                get
                {
                    return this.quantityField;
                }
                set
                {
                    this.quantityField = value;
                }
            }
    
            /// <remarks/>
            public double Discount
            {
                get
                {
                    return this.discountField;
                }
                set
                {
                    this.discountField = value;
                }
            }
        }
    
    
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Saturday, August 18, 2018 7:02 PM
    Thursday, August 16, 2018 3:08 AM
    Moderator
  • I have done the job this way

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    using System.Xml;
    using System.IO;
    
    namespace List2XML
    {
        public partial class OrderAndOrderDetails : Form
        {
            //string xml = "<Root> <Orders> <OrderID>10248</OrderID> <CustomerID>VINET</CustomerID> <EmployeeID>5</EmployeeID> <OrderDate>1996-07-04T00:00:00</OrderDate> <RequiredDate>1996-08-01T00:00:00</RequiredDate> <ShippedDate>1996-07-16T00:00:00</ShippedDate> <ShipVia>3</ShipVia> <Freight>32.3800</Freight> <ShipName>Vins et alcools Chevalier</ShipName> <ShipAddress>59 rue de l'Abbaye</ShipAddress> <ShipCity>Reims</ShipCity> <ShipPostalCode>51100</ShipPostalCode> <ShipCountry>France</ShipCountry> <OrderDetails> <OrderID>10248</OrderID> <ProductID>11</ProductID> <UnitPrice>14.0000</UnitPrice> <Quantity>12</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10248</OrderID> <ProductID>42</ProductID> <UnitPrice>9.8000</UnitPrice> <Quantity>10</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10248</OrderID> <ProductID>72</ProductID> <UnitPrice>34.8000</UnitPrice> <Quantity>5</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> </Orders> <Orders> <OrderID>10249</OrderID> <CustomerID>TOMSP</CustomerID> <EmployeeID>6</EmployeeID> <OrderDate>1996-07-05T00:00:00</OrderDate> <RequiredDate>1996-08-16T00:00:00</RequiredDate> <ShippedDate>1996-07-10T00:00:00</ShippedDate> <ShipVia>1</ShipVia> <Freight>11.6100</Freight> <ShipName>Toms Spezialitäten</ShipName> <ShipAddress>Luisenstr. 48</ShipAddress> <ShipCity>Münster</ShipCity> <ShipPostalCode>44087</ShipPostalCode> <ShipCountry>Germany</ShipCountry> <OrderDetails> <OrderID>10249</OrderID> <ProductID>14</ProductID> <UnitPrice>18.6000</UnitPrice> <Quantity>9</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10249</OrderID> <ProductID>51</ProductID> <UnitPrice>42.4000</UnitPrice> <Quantity>40</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> </Orders> <Orders> <OrderID>10250</OrderID> <CustomerID>HANAR</CustomerID> <EmployeeID>4</EmployeeID> <OrderDate>1996-07-08T00:00:00</OrderDate> <RequiredDate>1996-08-05T00:00:00</RequiredDate> <ShippedDate>1996-07-12T00:00:00</ShippedDate> <ShipVia>2</ShipVia> <Freight>65.8300</Freight> <ShipName>Hanari Carnes</ShipName> <ShipAddress>Rua do Paço, 67</ShipAddress> <ShipCity>Rio de Janeiro</ShipCity> <ShipRegion>RJ</ShipRegion> <ShipPostalCode>05454-876</ShipPostalCode> <ShipCountry>Brazil</ShipCountry> <OrderDetails> <OrderID>10250</OrderID> <ProductID>41</ProductID> <UnitPrice>7.7000</UnitPrice> <Quantity>10</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10250</OrderID> <ProductID>51</ProductID> <UnitPrice>42.4000</UnitPrice> <Quantity>35</Quantity> <Discount>1.5000001e-001</Discount> </OrderDetails> <OrderDetails> <OrderID>10250</OrderID> <ProductID>65</ProductID> <UnitPrice>16.8000</UnitPrice> <Quantity>15</Quantity> <Discount>1.5000001e-001</Discount> </OrderDetails> </Orders> </Root>";
            string xml = @"<Root><Orders><OrderID>10248</OrderID><CustomerID>VINET</CustomerID>
                <OrderDetails><OrderID>10248</OrderID><ProductID>11</ProductID><UnitPrice>14.0000</UnitPrice><Quantity>12</Quantity></OrderDetails>
                </Orders></Root>";
    
            public OrderAndOrderDetails()
            {
                InitializeComponent();
            }
    
            private void btnSerialize_Click(object sender, EventArgs e)
            {
                var serializer = new XmlSerializer(typeof(Root));
                Root result=new Root();
                result.Orders = new List<Orders>();
                result.Orders.Add(
                    new Orders()
                    {
                        OrderID = "101",
                        CustomerID = "001", 
                        OrderDetails = new List<OrderDetails>()
                        { 
                            new OrderDetails
                            { 
                                OrderID="101",
                                ProductID="1",
                                UnitPrice=20.25,
                                Quantity=5
                            }
                        } 
                    });
    
                result.Orders.Add(
                    new Orders()
                    {
                        OrderID = "102",
                        CustomerID = "002",
                        OrderDetails = new List<OrderDetails>()
                        { 
                            new OrderDetails
                            { 
                                OrderID="102",
                                ProductID="2",
                                UnitPrice=11,
                                Quantity=5
                            }
                        }
                    });
    
                var stringwriter = new System.IO.StringWriter();
                var serializerToString = new XmlSerializer(typeof(Root));
                serializer.Serialize(stringwriter, result);
                var stringResult = stringwriter.ToString();
            }
    
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                var serializer = new XmlSerializer(typeof(Root));
                Root result;
    
                using (TextReader reader = new StringReader(xml))
                {
                    result = (Root)serializer.Deserialize(reader);
                }
            }
        }
    
        [XmlRoot(ElementName = "OrderDetails")]
        public class OrderDetails
        {
            [XmlElement(ElementName = "OrderID")]
            public string OrderID { get; set; }
            [XmlElement(ElementName = "ProductID")]
            public string ProductID { get; set; }
            [XmlElement(ElementName = "UnitPrice")]
            public double UnitPrice { get; set; }
            [XmlElement(ElementName = "Quantity")]
            public int Quantity { get; set; }
        }
    
        [XmlRoot(ElementName = "Orders")]
        public class Orders
        {
            [XmlElement(ElementName = "OrderID")]
            public string OrderID { get; set; }
            [XmlElement(ElementName = "CustomerID")]
            public string CustomerID { get; set; }
    
            [XmlElement(ElementName = "OrderDetails")]
            public List<OrderDetails> OrderDetails { get; set; }
        }
    
        [XmlRoot(ElementName = "Root")]
        public class Root
        {
            [XmlElement(ElementName = "Orders")]
            public List<Orders> Orders { get; set; }
        }
    }
    

    • Marked as answer by Sudip_inn Saturday, August 18, 2018 7:02 PM
    Saturday, August 18, 2018 7:02 PM

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, if you wanna the classes of the xml which is stored in string variable, you could try the way below.

    Copy the xml data> Click Edit in Visual Studio> Choose Paste Special> Paste XML As Classes

    After that you could get the classes you want.

    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Root
        {
    
            private RootOrders[] ordersField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Orders")]
            public RootOrders[] Orders
            {
                get
                {
                    return this.ordersField;
                }
                set
                {
                    this.ordersField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrders
        {
    
            private ushort orderIDField;
    
            private string customerIDField;
    
            private byte employeeIDField;
    
            private System.DateTime orderDateField;
    
            private System.DateTime requiredDateField;
    
            private System.DateTime shippedDateField;
    
            private byte shipViaField;
    
            private decimal freightField;
    
            private string shipNameField;
    
            private string shipAddressField;
    
            private string shipCityField;
    
            private string shipRegionField;
    
            private string shipPostalCodeField;
    
            private string shipCountryField;
    
            private RootOrdersOrderDetails[] orderDetailsField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public string CustomerID
            {
                get
                {
                    return this.customerIDField;
                }
                set
                {
                    this.customerIDField = value;
                }
            }
    
            /// <remarks/>
            public byte EmployeeID
            {
                get
                {
                    return this.employeeIDField;
                }
                set
                {
                    this.employeeIDField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime OrderDate
            {
                get
                {
                    return this.orderDateField;
                }
                set
                {
                    this.orderDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime RequiredDate
            {
                get
                {
                    return this.requiredDateField;
                }
                set
                {
                    this.requiredDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime ShippedDate
            {
                get
                {
                    return this.shippedDateField;
                }
                set
                {
                    this.shippedDateField = value;
                }
            }
    
            /// <remarks/>
            public byte ShipVia
            {
                get
                {
                    return this.shipViaField;
                }
                set
                {
                    this.shipViaField = value;
                }
            }
    
            /// <remarks/>
            public decimal Freight
            {
                get
                {
                    return this.freightField;
                }
                set
                {
                    this.freightField = value;
                }
            }
    
            /// <remarks/>
            public string ShipName
            {
                get
                {
                    return this.shipNameField;
                }
                set
                {
                    this.shipNameField = value;
                }
            }
    
            /// <remarks/>
            public string ShipAddress
            {
                get
                {
                    return this.shipAddressField;
                }
                set
                {
                    this.shipAddressField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCity
            {
                get
                {
                    return this.shipCityField;
                }
                set
                {
                    this.shipCityField = value;
                }
            }
    
            /// <remarks/>
            public string ShipRegion
            {
                get
                {
                    return this.shipRegionField;
                }
                set
                {
                    this.shipRegionField = value;
                }
            }
    
            /// <remarks/>
            public string ShipPostalCode
            {
                get
                {
                    return this.shipPostalCodeField;
                }
                set
                {
                    this.shipPostalCodeField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCountry
            {
                get
                {
                    return this.shipCountryField;
                }
                set
                {
                    this.shipCountryField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("OrderDetails")]
            public RootOrdersOrderDetails[] OrderDetails
            {
                get
                {
                    return this.orderDetailsField;
                }
                set
                {
                    this.orderDetailsField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrdersOrderDetails
        {
    
            private ushort orderIDField;
    
            private byte productIDField;
    
            private decimal unitPriceField;
    
            private byte quantityField;
    
            private double discountField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public byte ProductID
            {
                get
                {
                    return this.productIDField;
                }
                set
                {
                    this.productIDField = value;
                }
            }
    
            /// <remarks/>
            public decimal UnitPrice
            {
                get
                {
                    return this.unitPriceField;
                }
                set
                {
                    this.unitPriceField = value;
                }
            }
    
            /// <remarks/>
            public byte Quantity
            {
                get
                {
                    return this.quantityField;
                }
                set
                {
                    this.quantityField = value;
                }
            }
    
            /// <remarks/>
            public double Discount
            {
                get
                {
                    return this.discountField;
                }
                set
                {
                    this.discountField = value;
                }
            }
        }
    
    

    There are a lot of data in your xml string.

    If you want to deserialize xml string, I use another xml string make a simple code for your reference.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApp
    {
        class Deserialization_XML_string
        {
            static void Main(string[] args)
            {
                // Prepare XML string
                string xmlString = "<Products><Product><Id>1</Id><Name>My XML product</Name></Product><Product><Id>2</Id><Name>My second product</Name></Product></Products>";
                //Create XML serializer
                XmlSerializer serializer = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("Products"));
                //Create StringReader object
                StringReader stringReader = new StringReader(xmlString);
                //deserialize to your C# object
                List<Product> productList = (List<Product>)serializer.Deserialize(stringReader);
            }
    
        }
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
    

    According to your xml data, I suggest you to read xml from xml file. Here is a simple example for your reference.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApp
    {
        class Deserialization_XML_strings2
        {
            static void Main(string[] args)
            {
                string filename = @"C:\Users\wendyz.WICRESOFT.000\OneDrive\Projects\VS2017 Project\ConsoleApp\ConsoleApp\Deserialization XML strings2.xml";
                // Create an instance of the XmlSerializer specifying type and namespace.
                XmlSerializer serializer = new
                XmlSerializer(typeof(Root));
    
                // A FileStream is needed to read the XML document.
                FileStream fs = new FileStream(filename, FileMode.Open);
                XmlReader reader = XmlReader.Create(fs);
    
                // Declare an object variable of the type to be deserialized.
                Root R;
    
                // Use the Deserialize method to restore the object's state.
                R = (Root)serializer.Deserialize(reader);
                fs.Close();
                foreach (var item in R.Orders)
                {
                    Console.WriteLine(item.CustomerID);
                }
                
                Console.ReadKey();
    
            }
        }
    
        // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Root
        {
    
            private RootOrders[] ordersField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Orders")]
            public RootOrders[] Orders
            {
                get
                {
                    return this.ordersField;
                }
                set
                {
                    this.ordersField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrders
        {
    
            private ushort orderIDField;
    
            private string customerIDField;
    
            private byte employeeIDField;
    
            private System.DateTime orderDateField;
    
            private System.DateTime requiredDateField;
    
            private System.DateTime shippedDateField;
    
            private byte shipViaField;
    
            private decimal freightField;
    
            private string shipNameField;
    
            private string shipAddressField;
    
            private string shipCityField;
    
            private string shipRegionField;
    
            private string shipPostalCodeField;
    
            private string shipCountryField;
    
            private RootOrdersOrderDetails[] orderDetailsField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public string CustomerID
            {
                get
                {
                    return this.customerIDField;
                }
                set
                {
                    this.customerIDField = value;
                }
            }
    
            /// <remarks/>
            public byte EmployeeID
            {
                get
                {
                    return this.employeeIDField;
                }
                set
                {
                    this.employeeIDField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime OrderDate
            {
                get
                {
                    return this.orderDateField;
                }
                set
                {
                    this.orderDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime RequiredDate
            {
                get
                {
                    return this.requiredDateField;
                }
                set
                {
                    this.requiredDateField = value;
                }
            }
    
            /// <remarks/>
            public System.DateTime ShippedDate
            {
                get
                {
                    return this.shippedDateField;
                }
                set
                {
                    this.shippedDateField = value;
                }
            }
    
            /// <remarks/>
            public byte ShipVia
            {
                get
                {
                    return this.shipViaField;
                }
                set
                {
                    this.shipViaField = value;
                }
            }
    
            /// <remarks/>
            public decimal Freight
            {
                get
                {
                    return this.freightField;
                }
                set
                {
                    this.freightField = value;
                }
            }
    
            /// <remarks/>
            public string ShipName
            {
                get
                {
                    return this.shipNameField;
                }
                set
                {
                    this.shipNameField = value;
                }
            }
    
            /// <remarks/>
            public string ShipAddress
            {
                get
                {
                    return this.shipAddressField;
                }
                set
                {
                    this.shipAddressField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCity
            {
                get
                {
                    return this.shipCityField;
                }
                set
                {
                    this.shipCityField = value;
                }
            }
    
            /// <remarks/>
            public string ShipRegion
            {
                get
                {
                    return this.shipRegionField;
                }
                set
                {
                    this.shipRegionField = value;
                }
            }
    
            /// <remarks/>
            public string ShipPostalCode
            {
                get
                {
                    return this.shipPostalCodeField;
                }
                set
                {
                    this.shipPostalCodeField = value;
                }
            }
    
            /// <remarks/>
            public string ShipCountry
            {
                get
                {
                    return this.shipCountryField;
                }
                set
                {
                    this.shipCountryField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("OrderDetails")]
            public RootOrdersOrderDetails[] OrderDetails
            {
                get
                {
                    return this.orderDetailsField;
                }
                set
                {
                    this.orderDetailsField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class RootOrdersOrderDetails
        {
    
            private ushort orderIDField;
    
            private byte productIDField;
    
            private decimal unitPriceField;
    
            private byte quantityField;
    
            private double discountField;
    
            /// <remarks/>
            public ushort OrderID
            {
                get
                {
                    return this.orderIDField;
                }
                set
                {
                    this.orderIDField = value;
                }
            }
    
            /// <remarks/>
            public byte ProductID
            {
                get
                {
                    return this.productIDField;
                }
                set
                {
                    this.productIDField = value;
                }
            }
    
            /// <remarks/>
            public decimal UnitPrice
            {
                get
                {
                    return this.unitPriceField;
                }
                set
                {
                    this.unitPriceField = value;
                }
            }
    
            /// <remarks/>
            public byte Quantity
            {
                get
                {
                    return this.quantityField;
                }
                set
                {
                    this.quantityField = value;
                }
            }
    
            /// <remarks/>
            public double Discount
            {
                get
                {
                    return this.discountField;
                }
                set
                {
                    this.discountField = value;
                }
            }
        }
    
    
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Saturday, August 18, 2018 7:02 PM
    Thursday, August 16, 2018 3:08 AM
    Moderator
  • I have done the job this way

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    using System.Xml;
    using System.IO;
    
    namespace List2XML
    {
        public partial class OrderAndOrderDetails : Form
        {
            //string xml = "<Root> <Orders> <OrderID>10248</OrderID> <CustomerID>VINET</CustomerID> <EmployeeID>5</EmployeeID> <OrderDate>1996-07-04T00:00:00</OrderDate> <RequiredDate>1996-08-01T00:00:00</RequiredDate> <ShippedDate>1996-07-16T00:00:00</ShippedDate> <ShipVia>3</ShipVia> <Freight>32.3800</Freight> <ShipName>Vins et alcools Chevalier</ShipName> <ShipAddress>59 rue de l'Abbaye</ShipAddress> <ShipCity>Reims</ShipCity> <ShipPostalCode>51100</ShipPostalCode> <ShipCountry>France</ShipCountry> <OrderDetails> <OrderID>10248</OrderID> <ProductID>11</ProductID> <UnitPrice>14.0000</UnitPrice> <Quantity>12</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10248</OrderID> <ProductID>42</ProductID> <UnitPrice>9.8000</UnitPrice> <Quantity>10</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10248</OrderID> <ProductID>72</ProductID> <UnitPrice>34.8000</UnitPrice> <Quantity>5</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> </Orders> <Orders> <OrderID>10249</OrderID> <CustomerID>TOMSP</CustomerID> <EmployeeID>6</EmployeeID> <OrderDate>1996-07-05T00:00:00</OrderDate> <RequiredDate>1996-08-16T00:00:00</RequiredDate> <ShippedDate>1996-07-10T00:00:00</ShippedDate> <ShipVia>1</ShipVia> <Freight>11.6100</Freight> <ShipName>Toms Spezialitäten</ShipName> <ShipAddress>Luisenstr. 48</ShipAddress> <ShipCity>Münster</ShipCity> <ShipPostalCode>44087</ShipPostalCode> <ShipCountry>Germany</ShipCountry> <OrderDetails> <OrderID>10249</OrderID> <ProductID>14</ProductID> <UnitPrice>18.6000</UnitPrice> <Quantity>9</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10249</OrderID> <ProductID>51</ProductID> <UnitPrice>42.4000</UnitPrice> <Quantity>40</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> </Orders> <Orders> <OrderID>10250</OrderID> <CustomerID>HANAR</CustomerID> <EmployeeID>4</EmployeeID> <OrderDate>1996-07-08T00:00:00</OrderDate> <RequiredDate>1996-08-05T00:00:00</RequiredDate> <ShippedDate>1996-07-12T00:00:00</ShippedDate> <ShipVia>2</ShipVia> <Freight>65.8300</Freight> <ShipName>Hanari Carnes</ShipName> <ShipAddress>Rua do Paço, 67</ShipAddress> <ShipCity>Rio de Janeiro</ShipCity> <ShipRegion>RJ</ShipRegion> <ShipPostalCode>05454-876</ShipPostalCode> <ShipCountry>Brazil</ShipCountry> <OrderDetails> <OrderID>10250</OrderID> <ProductID>41</ProductID> <UnitPrice>7.7000</UnitPrice> <Quantity>10</Quantity> <Discount>0.0000000e+000</Discount> </OrderDetails> <OrderDetails> <OrderID>10250</OrderID> <ProductID>51</ProductID> <UnitPrice>42.4000</UnitPrice> <Quantity>35</Quantity> <Discount>1.5000001e-001</Discount> </OrderDetails> <OrderDetails> <OrderID>10250</OrderID> <ProductID>65</ProductID> <UnitPrice>16.8000</UnitPrice> <Quantity>15</Quantity> <Discount>1.5000001e-001</Discount> </OrderDetails> </Orders> </Root>";
            string xml = @"<Root><Orders><OrderID>10248</OrderID><CustomerID>VINET</CustomerID>
                <OrderDetails><OrderID>10248</OrderID><ProductID>11</ProductID><UnitPrice>14.0000</UnitPrice><Quantity>12</Quantity></OrderDetails>
                </Orders></Root>";
    
            public OrderAndOrderDetails()
            {
                InitializeComponent();
            }
    
            private void btnSerialize_Click(object sender, EventArgs e)
            {
                var serializer = new XmlSerializer(typeof(Root));
                Root result=new Root();
                result.Orders = new List<Orders>();
                result.Orders.Add(
                    new Orders()
                    {
                        OrderID = "101",
                        CustomerID = "001", 
                        OrderDetails = new List<OrderDetails>()
                        { 
                            new OrderDetails
                            { 
                                OrderID="101",
                                ProductID="1",
                                UnitPrice=20.25,
                                Quantity=5
                            }
                        } 
                    });
    
                result.Orders.Add(
                    new Orders()
                    {
                        OrderID = "102",
                        CustomerID = "002",
                        OrderDetails = new List<OrderDetails>()
                        { 
                            new OrderDetails
                            { 
                                OrderID="102",
                                ProductID="2",
                                UnitPrice=11,
                                Quantity=5
                            }
                        }
                    });
    
                var stringwriter = new System.IO.StringWriter();
                var serializerToString = new XmlSerializer(typeof(Root));
                serializer.Serialize(stringwriter, result);
                var stringResult = stringwriter.ToString();
            }
    
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                var serializer = new XmlSerializer(typeof(Root));
                Root result;
    
                using (TextReader reader = new StringReader(xml))
                {
                    result = (Root)serializer.Deserialize(reader);
                }
            }
        }
    
        [XmlRoot(ElementName = "OrderDetails")]
        public class OrderDetails
        {
            [XmlElement(ElementName = "OrderID")]
            public string OrderID { get; set; }
            [XmlElement(ElementName = "ProductID")]
            public string ProductID { get; set; }
            [XmlElement(ElementName = "UnitPrice")]
            public double UnitPrice { get; set; }
            [XmlElement(ElementName = "Quantity")]
            public int Quantity { get; set; }
        }
    
        [XmlRoot(ElementName = "Orders")]
        public class Orders
        {
            [XmlElement(ElementName = "OrderID")]
            public string OrderID { get; set; }
            [XmlElement(ElementName = "CustomerID")]
            public string CustomerID { get; set; }
    
            [XmlElement(ElementName = "OrderDetails")]
            public List<OrderDetails> OrderDetails { get; set; }
        }
    
        [XmlRoot(ElementName = "Root")]
        public class Root
        {
            [XmlElement(ElementName = "Orders")]
            public List<Orders> Orders { get; set; }
        }
    }
    

    • Marked as answer by Sudip_inn Saturday, August 18, 2018 7:02 PM
    Saturday, August 18, 2018 7:02 PM