Last active
August 27, 2018 18:18
-
-
Save JonathanLoscalzo/fb31c46a73ead757e0b66e2ceae36892 to your computer and use it in GitHub Desktop.
xml serializer c# from: https://www.codeproject.com/Articles/1163664/Convert-XML-to-Csharp-Object. Use https://stackoverflow.com/questions/3187444/convert-xml-string-to-object for obtain the xml file first time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Customer> | |
| <CustomerID>ALFKI</CustomerID> | |
| <CompanyName>Alfreds Futterkiste</CompanyName> | |
| <ContactName>Maria Anders</ContactName> | |
| <ContactTitle>Sales Representative</ContactTitle> | |
| <Address>Obere Str. 57</Address> | |
| <City>Berlin</City> | |
| <PostalCode>12209</PostalCode> | |
| <Country>Germany</Country> | |
| <Phone>030-0074321</Phone> | |
| <Fax>030-0076545</Fax> | |
| </Customer> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Customer> | |
| <CustomerID>ALFKI</CustomerID> | |
| <CompanyName>Alfreds Futterkiste</CompanyName> | |
| <ContactName>Maria Anders</ContactName> | |
| <ContactTitle>Sales Representative</ContactTitle> | |
| <Address>Obere Str. 57</Address> | |
| <City>Berlin</City> | |
| <PostalCode>12209</PostalCode> | |
| <Country>Germany</Country> | |
| <Phone>030-0074321</Phone> | |
| <Fax>030-0076545</Fax> | |
| <Orders> | |
| <Order> | |
| <OrderID>10643</OrderID> | |
| <CustomerID>ALFKI</CustomerID> | |
| <EmployeeID>6</EmployeeID> | |
| <OrderDate>1997-08-25T00:00:00</OrderDate> | |
| <RequiredDate>1997-09-22T00:00:00</RequiredDate> | |
| <ShippedDate>1997-09-02T00:00:00</ShippedDate> | |
| <ShipVia>1</ShipVia> | |
| <Freight>29.4600</Freight> | |
| <ShipName>Alfreds Futterkiste</ShipName> | |
| <ShipAddress>Obere Str. 57</ShipAddress> | |
| <ShipCity>Berlin</ShipCity> | |
| <ShipPostalCode>12209</ShipPostalCode> | |
| <ShipCountry>Germany</ShipCountry> | |
| <Order_Details> | |
| <Order_Detail> | |
| <OrderID>10643</OrderID> | |
| <ProductID>28</ProductID> | |
| <UnitPrice>45.6000</UnitPrice> | |
| <Quantity>15</Quantity> | |
| <Discount>2.5000000e-001</Discount> | |
| <Product> | |
| <ProductID>28</ProductID> | |
| <ProductName>Rössle Sauerkraut</ProductName> | |
| <SupplierID>12</SupplierID> | |
| <CategoryID>7</CategoryID> | |
| <QuantityPerUnit>25 - 825 g cans</QuantityPerUnit> | |
| <UnitPrice>45.6000</UnitPrice> | |
| <UnitsInStock>26</UnitsInStock> | |
| <UnitsOnOrder>0</UnitsOnOrder> | |
| <ReorderLevel>0</ReorderLevel> | |
| <Discontinued>1</Discontinued> | |
| </Product> | |
| </Order_Detail> | |
| <Order_Detail> | |
| <OrderID>10643</OrderID> | |
| <ProductID>39</ProductID> | |
| <UnitPrice>18.0000</UnitPrice> | |
| <Quantity>21</Quantity> | |
| <Discount>2.5000000e-001</Discount> | |
| <Product> | |
| <ProductID>39</ProductID> | |
| <ProductName>Chartreuse verte</ProductName> | |
| <SupplierID>18</SupplierID> | |
| <CategoryID>1</CategoryID> | |
| <QuantityPerUnit>750 cc per bottle</QuantityPerUnit> | |
| <UnitPrice>18.0000</UnitPrice> | |
| <UnitsInStock>69</UnitsInStock> | |
| <UnitsOnOrder>0</UnitsOnOrder> | |
| <ReorderLevel>5</ReorderLevel> | |
| <Discontinued>0</Discontinued> | |
| </Product> | |
| </Order_Detail> | |
| </Order_Details> | |
| </Order> | |
| </Orders> | |
| </Customer> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Serializer ser = new Serializer(); | |
| string path = string.Empty; | |
| string xmlInputData = string.Empty; | |
| string xmlOutputData = string.Empty; | |
| // EXAMPLE 1 | |
| path = Directory.GetCurrentDirectory() + @"\Customer.xml"; | |
| xmlInputData = File.ReadAllText(path); | |
| Customer customer = ser.Deserialize<Customer>(xmlInputData); | |
| xmlOutputData = ser.Serialize<Customer>(customer) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Serializer ser = new Serializer(); | |
| string path = string.Empty; | |
| string xmlInputData = string.Empty; | |
| string xmlOutputData = string.Empty; | |
| // EXAMPLE 2 | |
| path = Directory.GetCurrentDirectory() + @"\CustOrders.xml"; | |
| xmlInputData = File.ReadAllText(path); | |
| Customer customer2 = ser.Deserialize<Customer>(xmlInputData); | |
| xmlOutputData = ser.Serialize<Customer>(customer2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.IO; | |
| using System.Xml.Serialization; | |
| namespace TestHarness | |
| { | |
| public class Serializer | |
| { | |
| public T Deserialize<T>(string input) where T : class | |
| { | |
| System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T)); | |
| using (StringReader sr = new StringReader(input)) | |
| { | |
| return (T)ser.Deserialize(sr); | |
| } | |
| } | |
| public string Serialize<T>(T ObjectToSerialize) | |
| { | |
| XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType()); | |
| using (StringWriter textWriter = new StringWriter()) | |
| { | |
| xmlSerializer.Serialize(textWriter, ObjectToSerialize); | |
| return textWriter.ToString(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment