Returning XML natively in a .NET (C#) webservice?

Posted by James McMahon on Stack Overflow See other posts from Stack Overflow or by James McMahon
Published on 2009-01-30T16:44:03Z Indexed on 2010/04/20 8:43 UTC
Read the original article Hit count: 261

Filed under:
|
|
|

I realize that SOAP webservices in .NET return XML representation of whatever object the web method returns, but if I want to return data formatting in XML what is the best object to store it in?

I am using the answer to this question to write my XML, here is the code:

XmlWriter writer = XmlWriter.Create(pathToOutput);
writer.WriteStartDocument();
writer.WriteStartElement("People");

writer.WriteStartElement("Person");
writer.WriteAttributeString("Name", "Nick");
writer.WriteEndElement();

writer.WriteStartElement("Person");
writer.WriteStartAttribute("Name");
writer.WriteValue("Nick");
writer.WriteEndAttribute();
writer.WriteEndElement();

writer.WriteEndElement();
writer.WriteEndDocument();

writer.Flush();

Now I can return this output as a String to my calling webmethod, but it shows up as <string> XML HERE </string>, is there anyway to just return the full xml?

Please in your answer, give an example of how to use said object with either XmlWriter or another internal object (if you consider XmlWriter to be a poor choice). The System.Xml package (namespace) has many objects, but I haven't been able to uncover decent documentation on how to use the objects together, or what to use for what situations.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET