本机返回的XML在.NET(C#)web服务? [英] Returning XML natively in a .NET (C#) webservice?

查看:150
本文介绍了本机返回的XML在.NET(C#)web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现.NET返回的XML重新presentation的SOAP Web服务的任何对象的Web方法返回,但如果我要返回数据的XML格式是什么,将其存储的最佳对象?

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?

我使用的是回答以< A HREF =htt​​p://stackoverflow.com/questions/220867/how-to-deal-with-xml-in-c>这个问题给我写XML,这里是code:

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

现在我可以返回一个字符串我打电话的webmethod此输出,但它显示为&LT;字符串&GT; XML此处&lt; /串&GT; ,反正是有只返回完整的XML

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?

请在你的答案,给出了如何使用上述对象,无论是XmlWriter的或其他内部对象的示例(如果你考虑的XmlWriter是一个糟糕的选择)。对System.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.

推荐答案

这是怎么会出现这样做;

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.

这篇关于本机返回的XML在.NET(C#)web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆