Jump to content

.net xml deserialization


Recommended Posts

Posted

Bhayyas here is my xml. 

How can I deserialize it to an object but get rid of the Highlighted Car element and retain the child elements ? Here is how I'm doing right now. 

using (TextReader reader = new StringReader(stringXml))
{
    var result = (MyVehicleClass) serializer.Deserialize(reader);
}

<Vehicle>

<Truck/>

<Car>

<Car>

<make "Honda"/>

<Model "Accord"/>

</Car>

<Car>

<make "Toyota"/>

<Model "Camry"/>

</Car>

</Car>

</Vehicle>

public MyVehicleClass

{

public List<Car> cars {get; set;}

}

public Car

{

public string Make {get; set;}

public string Model {get; set;}

}

Posted

haha ledu bhayya 

nasty client has the same element twice and not sure how to remove the parent and keep the child elements 

Posted

You ned to create a class first

serialize that xml in to class object

use “newtonsoft” package for json serialization

Posted
1 minute ago, dasari4kntr said:

You ned to create a class first

serialize that xml in to class object

use “newtonsoft” package for json serialization

already serializing bhayya but since Car is both parent and child It is not being deserialized correctly 

XmlSerializer serializer = new XmlSerializer(typeof(MyVehicleClass));

Posted
1 minute ago, bevarse said:

already serializing bhayya but since Car is both parent and child It is not being deserialized correctly 

XmlSerializer serializer = new XmlSerializer(typeof(MyVehicleClass));

Isnt it parent car suppose tobe cars?

Posted
3 minutes ago, dasari4kntr said:

Isnt it parent car suppose tobe cars?

it would have been easier that way but the vendor had both the parent and child elements as CAR

 

Posted
3 minutes ago, bevarse said:

it would have been easier that way but the vendor had both the parent and child elements as CAR

 

Not sure this will help or not for you ...did u try the class like this 

 

public class car {
 Public string make {get; set; }

Public string model {get; set; }

Public list<car> car {get; set; }

}

Posted

Generate xsd from xml 

Generate classes from xsd

Then do all serialization

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...