locked
How to serialize deserialize class to xml RRS feed

  • Question

  • User-466819921 posted

    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.

    <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>



    Wednesday, August 15, 2018 6:13 PM

Answers

  • User1724605321 posted

    Hi dev_dona,

    Or you could use some online covert website like http://xmltocsharp.azurewebsites.net/ . Code below is for your reference :

        class Program
        {
            static void Main(string[] args)
            {
                //Convert xml string to object
                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>";
                var serializer = new XmlSerializer(typeof(Root));
                Root result;
    
                using (TextReader reader = new StringReader(xml))
                {
                    result = (Root)serializer.Deserialize(reader);
                }
    
                //Convert object to xml string 
    
                var stringwriter = new System.IO.StringWriter();
                var serializerToString = new XmlSerializer(typeof(Root));
                serializer.Serialize(stringwriter, result);
                var stringResult= stringwriter.ToString();
           
                Console.ReadKey();
            }
        }
    
        [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 string UnitPrice { get; set; }
            [XmlElement(ElementName = "Quantity")]
            public string Quantity { get; set; }
            [XmlElement(ElementName = "Discount")]
            public string Discount { 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 = "EmployeeID")]
            public string EmployeeID { get; set; }
            [XmlElement(ElementName = "OrderDate")]
            public string OrderDate { get; set; }
            [XmlElement(ElementName = "RequiredDate")]
            public string RequiredDate { get; set; }
            [XmlElement(ElementName = "ShippedDate")]
            public string ShippedDate { get; set; }
            [XmlElement(ElementName = "ShipVia")]
            public string ShipVia { get; set; }
            [XmlElement(ElementName = "Freight")]
            public string Freight { get; set; }
            [XmlElement(ElementName = "ShipName")]
            public string ShipName { get; set; }
            [XmlElement(ElementName = "ShipAddress")]
            public string ShipAddress { get; set; }
            [XmlElement(ElementName = "ShipCity")]
            public string ShipCity { get; set; }
            [XmlElement(ElementName = "ShipPostalCode")]
            public string ShipPostalCode { get; set; }
            [XmlElement(ElementName = "ShipCountry")]
            public string ShipCountry { get; set; }
            [XmlElement(ElementName = "OrderDetails")]
            public List<OrderDetails> OrderDetails { get; set; }
            [XmlElement(ElementName = "ShipRegion")]
            public string ShipRegion { get; set; }
        }
    
        [XmlRoot(ElementName = "Root")]
        public class Root
        {
            [XmlElement(ElementName = "Orders")]
            public List<Orders> Orders { get; set; }
        }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2018 6:29 AM

All replies

  • User475983607 posted

    Start by reading the .NET XML API reference documentation.  There are tons of code examples.

    https://docs.microsoft.com/en-us/dotnet/standard/data/xml/

    suppose below master detail xml is stored in string variable. now how to write class

    Create a new class file in Visual Studio.  Copy the XMl to the clipboard.  Place place the cursor in the new class file in visual Studio.  Click the Edit menu -> Paste Special ->  Paste as XML Classes.

    You can also simply paste the XML in a Visual Studio XML file and take advantage of the XML tools in Visual Studio which are very extensive.  for example creating schema files.

    Wednesday, August 15, 2018 6:49 PM
  • User1724605321 posted

    Hi dev_dona,

    Or you could use some online covert website like http://xmltocsharp.azurewebsites.net/ . Code below is for your reference :

        class Program
        {
            static void Main(string[] args)
            {
                //Convert xml string to object
                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>";
                var serializer = new XmlSerializer(typeof(Root));
                Root result;
    
                using (TextReader reader = new StringReader(xml))
                {
                    result = (Root)serializer.Deserialize(reader);
                }
    
                //Convert object to xml string 
    
                var stringwriter = new System.IO.StringWriter();
                var serializerToString = new XmlSerializer(typeof(Root));
                serializer.Serialize(stringwriter, result);
                var stringResult= stringwriter.ToString();
           
                Console.ReadKey();
            }
        }
    
        [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 string UnitPrice { get; set; }
            [XmlElement(ElementName = "Quantity")]
            public string Quantity { get; set; }
            [XmlElement(ElementName = "Discount")]
            public string Discount { 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 = "EmployeeID")]
            public string EmployeeID { get; set; }
            [XmlElement(ElementName = "OrderDate")]
            public string OrderDate { get; set; }
            [XmlElement(ElementName = "RequiredDate")]
            public string RequiredDate { get; set; }
            [XmlElement(ElementName = "ShippedDate")]
            public string ShippedDate { get; set; }
            [XmlElement(ElementName = "ShipVia")]
            public string ShipVia { get; set; }
            [XmlElement(ElementName = "Freight")]
            public string Freight { get; set; }
            [XmlElement(ElementName = "ShipName")]
            public string ShipName { get; set; }
            [XmlElement(ElementName = "ShipAddress")]
            public string ShipAddress { get; set; }
            [XmlElement(ElementName = "ShipCity")]
            public string ShipCity { get; set; }
            [XmlElement(ElementName = "ShipPostalCode")]
            public string ShipPostalCode { get; set; }
            [XmlElement(ElementName = "ShipCountry")]
            public string ShipCountry { get; set; }
            [XmlElement(ElementName = "OrderDetails")]
            public List<OrderDetails> OrderDetails { get; set; }
            [XmlElement(ElementName = "ShipRegion")]
            public string ShipRegion { get; set; }
        }
    
        [XmlRoot(ElementName = "Root")]
        public class Root
        {
            [XmlElement(ElementName = "Orders")]
            public List<Orders> Orders { get; set; }
        }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2018 6:29 AM
  • User-466819921 posted

    thanks for nice example and help

    see this area

    [XmlElement(ElementName = "OrderDetails")]
    public List<OrderDetails> OrderDetails { get; set; }

    can't we use here XmlArray instead of XmlElement for order details because each order will have multiple order details ?

    [XmlArray("Staff")]
    [XmlArrayItem("StaffMember")]

    if i use XmlArray instead of XmlElement then generated xml would look different ?

    looking for your further guidance. thanks

    Saturday, August 18, 2018 5:09 PM
  • User-466819921 posted

    I copied your code in my project but when try to serialize then getting this error

    1. An unhandled exception of type 'System.NullReferenceException' occurred in TestXML.exe
    2. Additional information: Object reference not set to an instance of an object.

    here is my serialization code

                var serializer = new XmlSerializer(typeof(Root));
                Root result=new Root();
                result.Orders.Add(
                    new Orders { OrderID = "101", CustomerID = "001", 
                        OrderDetails = new List<OrderDetails> 
                        { 
                            new OrderDetails
                            { 
                                OrderID="101",
                                ProductID="1",
                                UnitPrice=20.25,
                                Quantity=5
                            }
                        } 
                    });
    
    
                var stringwriter = new System.IO.StringWriter();
                var serializerToString = new XmlSerializer(typeof(Root));
                serializer.Serialize(stringwriter, result);
                var stringResult = stringwriter.ToString();

    so tell me where i made the mistake in code ?

    also i like to tell you that i comment few properties of order and order detail class.

    Object reference Error has been fixed.

                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();
    Saturday, August 18, 2018 5:35 PM