How to add comment to end of XmlDocument in C# when returned from ASMX Webservice

1k views Asked by At

I have the following class;

[XmlRoot("Customer")]
public class MyClass
{
    [XmlElement("CustId")]
    public int Id {get;set;}

    [XmlElement("CustName")]
    public string Name {get;set;}
}

I then use the following function serialise the class object to Xml

public static XmlDocument SerializeObjectToXML(object obj, string sElementName)
 {
    XmlSerializer serializer = 
          new XmlSerializer(obj.GetType(), new XmlRootAttribute("Response"));

    using (MemoryStream ms = new MemoryStream())
    {
       XmlDocument xmlDoc = new XmlDocument();
       serializer.Serialize(ms, obj);
       ms.Position = 0;
       xmlDoc.Load(ms);
    }
}

My current output to XML is like;

<Response>
  <CustId></CustId>
  <CustName></CustName>
</Response>

However I'd like to add a comment like

<Response>
  <CustId></CustId>
  <CustName></CustName>
</Response>
<!-- Sample Comment Here -->

How can I achieve this ? I tried the following;

XmlComment xmlComment;
xmlComment = xmlDoc.CreateComment("Sample XML document");

XmlElement root = xmlDoc.DocumentElement;
xmlDoc.InsertAfter(xmlComment, root);

But, when I try to read the Xml using this kind of WebClient call

oClient.UploadString("http://www.myurl.com/", "POST", "");

I can see the Xml elements, but not the comment.

UPDATE

I checked, and even if I call the webservice (ASMX) directly via a browser, the comment tag is NOT returned when using the browser developer Tools.

It would appear that the ASMX webservice is not returning the comment tag...?

0

There are 0 answers