views:

5534

answers:

5

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.

+4  A: 

Just return a XmlDocument. e.g.

[WebMethod]
public XmlDocument Quux()
{

}

HTH
Kev

Kev
Yeah I was looking into that, but I don't understand how XmlWriter interacts with XmlDocument.
James McMahon
Why the down vote?
Kev
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
Thank you for trying to help, but this wasn't the answer I was looking for.
James McMahon
A: 

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

Oscar Cabrero
+2  A: 

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
That's exactly how to do it.
Kev
You might want wrap your XmlWriter in a using statement: using(XmlWriter writer = XmlWriter.Create(sb, settings)) { build xml and return xmldocument }
Kev
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
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
Hmm interesting, I am actually consuming the service through Java. That has its own share of interesting challenges.
James McMahon
...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
Sometimes I wish you could vote up comments, again thank you for your help.
James McMahon
no prbs's, glad to help :)
Kev
Dude, you are a champion
Yo Momma
A: 

XmlElement, not XmlDocument.

Return an XmlElement.

Cheeso
What is the benefit?
James McMahon
Make it clear that you are returning a fragment, not a complete document. There is no declaration, for example. and so on.
Cheeso
How would I load my XmlWriter data into an XmlElement? I played around with it briefly but couldn't figure it out.
James McMahon
A: 

Thx a lot for posting your solution here...saved me a lot of misery...I know its a year since the last post but your solution was spot-on...

Chetan