11

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.

Community
  • 1
  • 1
James McMahon
  • 48,506
  • 64
  • 207
  • 283

4 Answers4

14

This is how I ended up doing it;

StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);

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();

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(sb.ToString());
return xmlDocument;

May not be the best method, but it appears to be working. Let me know if you have a better method. Thanks.

James McMahon
  • 48,506
  • 64
  • 207
  • 283
  • 1
    You might want wrap your XmlWriter in a using statement: using(XmlWriter writer = XmlWriter.Create(sb, settings)) { build xml and return xmldocument } – Kev Jan 30 '09 at 19:53
  • Its actually in method that is called by the web method, so hopefully the CLR is garbage collecting that efficiently. Thanks for all your help Kev. – James McMahon Jan 30 '09 at 20:26
  • 1
    Also one thing to watch is that if when you do Add Web Reference to your web service (if your client is .NET based), the generated WS proxy code will return an XmlNode, not an XmlDocument. Just so you know. :) – Kev Jan 30 '09 at 20:29
  • Hmm interesting, I am actually consuming the service through Java. That has its own share of interesting challenges. – James McMahon Jan 30 '09 at 20:33
  • 2
    ...XmlDocument derives from XmlNode anyway so there's not much hardship in terms of functionality being taken away to query the xml using XPath or creating XPathNavigators etc. – Kev Jan 30 '09 at 20:35
  • Sometimes I wish you could vote up comments, again thank you for your help. – James McMahon Jan 30 '09 at 20:40
  • Should have given the answer to Kev. He told you to return an XmlDocument and that's what you're doing. Credit Fail – capdragon Mar 03 '11 at 21:30
  • I was wondering the same since Kev did mention to return the XMLDocument. Either ways, I found what I was looking for – hangar18 Sep 14 '11 at 13:42
12

Just return a XmlDocument. e.g.

[WebMethod]
public XmlDocument Quux()
{

}
Kev
  • 118,037
  • 53
  • 300
  • 385
  • Yeah I was looking into that, but I don't understand how XmlWriter interacts with XmlDocument. – James McMahon Jan 30 '09 at 18:34
  • It is a failing of the system, your answer was not what I was looking for (though it my failing for not being clear enough in my question, which I correct) so I down voted. But I can see you actively trying to help, so I removed the down vote. The system needs a way of saying... – James McMahon Jan 30 '09 at 20:29
  • Thank you for trying to help, but this wasn't the answer I was looking for. – James McMahon Jan 30 '09 at 20:31
1

XmlElement, not XmlDocument.

Return an XmlElement.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Make it clear that you are returning a fragment, not a complete document. There is no declaration, for example. and so on. – Cheeso Mar 02 '09 at 22:09
0

you can have a class that represent your XML and return that class or also return your xml inside an XMLNode

Oscar Cabrero
  • 4,168
  • 8
  • 29
  • 49