在 .NET (C#) 网络服务中本机返回 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/496222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:40:23  来源:igfitidea点击:

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

c#.netxmlweb-services

提问by James McMahon

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?

我意识到 .NET 中的 SOAP webservices 返回 web 方法返回的任何对象的 XML 表示,但是如果我想以 XML 格式返回数据格式,最好的对象是什么?

I am using the answerto this questionto write my XML, here is the code:

我正在使用这个问题答案来编写我的 XML,这是代码:

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?

现在我可以将此输出作为字符串返回给我的调用 webmethod,但它显示为<string> XML HERE </string>,无论如何只返回完整的 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.

请在您的回答中,举例说明如何将所述对象与 XmlWriter 或其他内部对象一起使用(如果您认为 XmlWriter 是一个糟糕的选择)。System.Xml 包(命名空间)有许多对象,但我一直无法找到有关如何一起使用这些对象或在什么情况下使用什么的体面文档。

采纳答案by James McMahon

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.

可能不是最好的方法,但它似乎有效。如果您有更好的方法,请告诉我。谢谢。

回答by Kev

Just return a XmlDocument. e.g.

只需返回一个 XmlDocument。例如

[WebMethod]
public XmlDocument Quux()
{

}

回答by Oscar Cabrero

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

您可以拥有一个代表您的 XML 的类并返回该类,或者在 XMLNode 中返回您的 xml

回答by Cheeso

XmlElement, not XmlDocument.

XmlElement,而不是 XmlDocument。

Return an XmlElement.

返回一个 XmlElement