user3743646
user3743646

Reputation: 53

wcf service - convert Object to System.Xml.XmlNode

I have a web service having a method which accepts id number as parameter and returns Object which contains his personal details. Now I need to convert this object which is of Object type into XmlNode. If I use:

XmlNode xml = (XmlNode)retObj; //here retObj is of type Object

then I am not getting nodes with values.

Please help me how can I get all the details..Please... Also I don't have to use SOAP....So i need solution where no code of SOAP is used

Upvotes: 0

Views: 1892

Answers (1)

invernomuto
invernomuto

Reputation: 10211

I assume you know the structure of object returned by your service, commonly you should map your object to a DTO as follow:

  public class MyObjDTO
  {
    public string Name { get;set;}
    public string DOB { get; set; }
    public string Nationaliy { get; set; }

  }

then you can serialize the DTO to string using an XMLSerializer:

  var dto = (MyObjDTO)retObj;
  XmlSerializer serializer = new XmlSerializer(typeof(MyObjDTO));
  StringWriter textWriter = new StringWriter();
  serializer.Serialize(textWriter, dto);

then obtain the XMLNode by XmlDocument

  var xmlString = textWriter.ToString();
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(xmlString);
  XmlNode newNode = doc.DocumentElement;

Upvotes: 2

Related Questions