Convert from dynamic xml to c # object

I need inputs to convert dynamic xml to a specific C # object model

My xml sample looks like this:

<?xml version="1.0" encoding="utf-8" ?> <Persons> <Person> <Id>10</Id> <FirstName> Dino </FirstName> <LastName> Esposito </LastName> <Addresses> <Address> <AddressType>BillTo</AddressType> <Street1></Street1> <Street2></Street2> <Street3></Street3> <City>Moscow</City> </Address> <Address> <AddressType>ShipTo</AddressType> <Street1></Street1> <Street2></Street2> <Street3></Street3> <City>Moscow</City> </Address> <Address> <AddressType>Contact</AddressType> <Street1></Street1> <Street2></Street2> <Street3></Street3> <City>Moscow</City> </Address> </Addresses> </Person> </Persons> 

I expect the values ​​of this xml to be converted to a C # object at runtime. I would like an object similar to the following to be defined: My expected C # class object is as follows:

 public class Person { public int Id { get; set; } public String FirstName { get; set; } public String LastName { get; set; } public IList<Address> Addresses { get; set; } } public class Address { public string AddressType { get; set; } public string Street1 { get; set; } public string Street2 { get; set; } public string Street3 { get; set; } public string City { get; set; } } 

I went through dynamic and ExpandoObject in C # 4.0, this approach allows me to dynamically retrieve values ​​using keys. I do not know how I can fill them in my datamodel.

Note. . I do not want to determine the structure of this class model, it is constantly changing over time. I am looking for a solution that allows me to get values ​​such as DynamicObject.Addresses.Address [0] .City.

Please provide your details.

+6
source share
5 answers

I would suggest you read this article: http://www.codeproject.com/Articles/62839/Adventures-with-C-4-0-dynamic-ExpandoObject-Elasti . There is a way to build a dynamic object from XML. Or this article: http://www.codeproject.com/Articles/461677/Creating-a-dynamic-object-from-XML-using-ExpandoOb . Take everything you need and you can customize the code for your needs.

+3
source

A solution using LINQ2XML might look like this (without classes):

 var xml = XDocument.Parse(xmlSrc); // from XML string, eg: <xml ...><Persons><Person>... //var xml = XDocument.Load(xmlFile); // from XML file, eg: c:\temp\persons.xml var persons = xml.Root.Elements("Person").ToList(); var p1Addresses = persons[0].Elements("Addresses").ToList(); foreach (var address in p1Addresses.Elements("Address")) { var elementAddress = address.Element("AddressType"); var elementCity = address.Element("City"); System.Console.WriteLine(string.Format("{0} - {1}", elementAddress.Value, elementCity.Value)); } 

Output:

 BillTo - Moscow ShipTo - Moscow Contact - Moscow 
+6
source

Check out this example:

  string xml = @"<?xml version='1.0' encoding='utf-8' ?> <Persons> <Person> <Id>10</Id> <FirstName> Dino </FirstName> <LastName> Esposito </LastName> <Addresses> <Address> <AddressType>BillTo</AddressType> <Street1></Street1> <Street2></Street2> <Street3></Street3> <City>Moscow</City> </Address> <Address> <AddressType>ShipTo</AddressType> <Street1></Street1> <Street2></Street2> <Street3></Street3> <City>Moscow</City> </Address> <Address> <AddressType>Contact</AddressType> <Street1></Street1> <Street2></Street2> <Street3></Street3> <City>Moscow</City> </Address> </Addresses> </Person> </Persons>"; XElement root = XElement.Parse(xml); IEnumerable<XElement> list = root.XPathSelectElements("./Person/Addresses/Address[2]/City"); foreach (XElement el in list) Console.WriteLine(el); Console.ReadLine(); 

You will receive: <City>Moscow</City>

Or look at this solution using DynamicObject :

  XElement root = XElement.Parse(xml); dynamic persons = DynamicXml.Parse(xml); Console.WriteLine(persons.Person.Addresses.Address[1].City); 

deserialize XML for an object using dynamic

+2
source

Thanks to everyone for your materials, the solution I'm looking for is available at this place http://www.codeproject.com/Tips/227139/Converting-XML-to-an-dynamic-object-using-ExpandoO .

+1
source

I created the project specifically for this purpose, as I was unhappy with the alternatives (such as the CodeProject article related to other answers).

I posted it on Github: https://github.com/jonathanconway/XmlToDynamic .

I would prefer not to put the source code here, since it is too large, but here it is used:

 var xmlString = @"<addressBook> <contacts> <contact> <name>Jonathan</name> </contact> </contacts> </addressBook>"; var dynamicXml = XElement.Parse(xmlString).ToDynamic(); var firstPersonsName = dynamicXml.contacts[0].name.Value; // firstPersonsName will be 'Jonathan' 
0
source

Source: https://habr.com/ru/post/945415/


All Articles