简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM