bevarse Posted March 6, 2020 Report Share Posted March 6, 2020 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;} } Quote Link to comment Share on other sites More sharing options...
ShruteSastry Posted March 6, 2020 Report Share Posted March 6, 2020 Git anukuni ikkada vesesava kompateesi. Quote Link to comment Share on other sites More sharing options...
bevarse Posted March 6, 2020 Author Report Share Posted March 6, 2020 haha ledu bhayya nasty client has the same element twice and not sure how to remove the parent and keep the child elements Quote Link to comment Share on other sites More sharing options...
dasari4kntr Posted March 6, 2020 Report Share Posted March 6, 2020 You ned to create a class first serialize that xml in to class object use “newtonsoft” package for json serialization Quote Link to comment Share on other sites More sharing options...
bevarse Posted March 6, 2020 Author Report Share Posted March 6, 2020 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)); Quote Link to comment Share on other sites More sharing options...
dasari4kntr Posted March 6, 2020 Report Share Posted March 6, 2020 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? Quote Link to comment Share on other sites More sharing options...
bevarse Posted March 6, 2020 Author Report Share Posted March 6, 2020 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 Quote Link to comment Share on other sites More sharing options...
dasari4kntr Posted March 6, 2020 Report Share Posted March 6, 2020 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; } } Quote Link to comment Share on other sites More sharing options...
alpachinao Posted March 6, 2020 Report Share Posted March 6, 2020 Generate xsd from xml Generate classes from xsd Then do all serialization Quote Link to comment Share on other sites More sharing options...
Suhaas Posted March 6, 2020 Report Share Posted March 6, 2020 You need to define your Data Model clearly Follow the answer in this thead https://stackoverflow.com/questions/52990725/deserialize-xml-with-xmlserializer-where-parent-and-child-have-the-same-tag Quote Link to comment Share on other sites More sharing options...
bevarse Posted March 6, 2020 Author Report Share Posted March 6, 2020 16 minutes ago, Suhaas said: You need to define your Data Model clearly Follow the answer in this thead https://stackoverflow.com/questions/52990725/deserialize-xml-with-xmlserializer-where-parent-and-child-have-the-same-tag excellent. this fixed the issue. thank you bro. this is the second or third time you are helping me Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.