1

There are 3 classes like this

public class Product
{
  public string ProudctId { get; set; }
  public string ProductName { get; set; }
}
public class Owner
{
  public string OwnerId { get; set; }
  public string OwnerName { get; set; }
}
public class Master
{
  public string MasterId { get; set; }
  public string MasterName { get; set; }
}

I've managed to convert these objects to XML string. Now,i want to get original objects from XML. XML looks like below:

<ProjectDetails>
   <Product>
       <ProductId>1</ProductId>
       <ProductName>Product 1</ProductName>
   </Product>
   <Owner>
       <OwnerId>1</OwnerId>
       <OwnerName>Owner 1</OwnerName>
   </Owner>
   <Master>
       <MasterId>1</MasterId>
       <MasterName>Master 1</MasterName>
   </Master>
</ProjectDetails>

Problem is, how to convert above xml into 3 objects? There is possibility that data for some of the objects may not be available in XML. e.g. Master node may or may not be available in XML. Root node name is fixed(ProjectDetails).

Any help is really appreciated.

Manish Dhariwal
  • 61
  • 1
  • 1
  • 9
  • 2
    `I've managed to convert these objects to XML string` How did you do that? Why can't you do it in reverse order? – Renatas M. Jul 09 '18 at 14:11
  • Did you look upon Xml-serialization? There are thousands of similar topics around. – MakePeaceGreatAgain Jul 09 '18 at 14:13
  • XmlSerializer, see https://stackoverflow.com/questions/4123590/serialize-an-object-to-xml – Greg Jul 09 '18 at 14:14
  • @Greg The problem is how to identify that This node should be translated to this object and so on. e.g. How to identify that Product node should be translated to object of class Product? In addition to that, the link you've posted shows how to convert object to xml which i've already done using XmlSerializer. I want to do the reverse process. – Manish Dhariwal Jul 09 '18 at 14:16
  • Look upon the proposed links. They show you how to achieve this. – MakePeaceGreatAgain Jul 09 '18 at 14:17
  • Check this out friend, it is exactly this. https://stackoverflow.com/q/608110/5874935 – Travis Tubbs Jul 09 '18 at 14:26

1 Answers1

0
var yourXML = @"<ProjectDetails>
       <Product>
           <ProductId>1</ProductId>
           <ProductName>Product 1</ProductName>
       </Product>
       <Owner>
           <OwnerId>1</OwnerId>
           <OwnerName>Owner 1</OwnerName>
       </Owner>
       <Master>
           <MasterId>1</MasterId>
           <MasterName>Master 1</MasterName>
       </Master>
    </ProjectDetails>"    

XmlSerializer serializer = new XmlSerializer(typeof(ProjectDetails));
using (TextReader reader = new StringReader(yourXML)) {
    ProjectDetails result = (ProjectDetails) serializer.Deserialize(reader);
}

Or if you wish to deserialize Product, Owner, Master independently, replace ProjectDetails with the type you are deserializing. Or create a generic helper function to deserialize any type. (see https://stackoverflow.com/a/3187539/6419909)

This is known as deserializing an object, and in your case XML deserialization. See How to deserialize xml to object for further discussion.

Mattkwish
  • 753
  • 7
  • 16