简体   繁体   English

如何从ASPX页面返回XML

[英]How to return XML from an ASPX page

I have an object and would like to convert it into XML and show it on page in its raw format. 我有一个对象,并希望将其转换为XML并以原始格式在页面上显示。

Desired output 期望的输出

<Response>
 <ResponseCode>100</ResponseCode>
 <ResponseDescription>Test</ResponseDescription>
</Reponse>

Code: 码:

public class Response
{
    public Response(){}

    public string ResponseCode { get; set; }        
    public string ResponseDescription { get; set; }
}

Page_Load()
{
 Response obj = new Response();
 obj.ResponseCode = "100";
 obj.ResponseDescription = "test";

 string xmlString;
 XmlSerializer serializer = new XmlSerializer(typeof(Response));
 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
 // exclude xsi and xsd namespaces by adding the following:
 ns.Add(string.Empty, string.Empty);

 using (StringWriter textWriter = new StringWriter())
  {
   using (XmlWriter xmlWriter = XmlWriter.Create(textWriter))
    {
      serializer.Serialize(xmlWriter, obj, ns);
    }
   xmlString = textWriter.ToString(); 
  }    
  Response.Write(xmlString);
}

The result is just like this. 结果就像这样。

100 test 100测试

What should I do to get the desired output. 我该怎么做才能获得所需的输出。

The actual problem is that you're outputting XML as HTML, causing your browser to treat the response as " tag soup " and try to render it as an HTML document. 实际问题是您将XML作为HTML输出,导致您的浏览器将响应视为“ 标记汤 ”并尝试将其呈现为HTML文档。

This hides all tags and their attributes, and only renders the text inside and between tags. 这会隐藏所有标记及其属性,并仅在标记内部和标记之间呈现文本。

This isn't HTML, it's XML. 这不是HTML,而是XML。 So the actual solution is to set the proper content-type, indicating that you're actually returning XML, from How do you specify your Content Type in ASP.NET WebForms? 因此,实际的解决方案是设置正确的内容类型,表明您实际上是在返回XML, 如何在ASP.NET WebForms中指定内容类型? :

Response.ContentType = "application/xml";

Additionally, you're asking to omit the XML declaration . 此外,您要求省略XML声明 From how to create an xml using xml writer without declaration element and MSDN: XmlWriterSettings.OmitXmlDeclaration : 如何使用没有声明元素的xml writerMSDN 创建一个xml :XmlWriterSettings.OmitXmlDeclaration

The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true. 如果将ConformanceLevel设置为Document,则始终会写入XML声明,即使OmitXmlDeclaration设置为true也是如此。

The XML declaration is never written if ConformanceLevel is set to Fragment 如果ConformanceLevel设置为Fragment,则永远不会写入XML声明

So simply set the settings.ConformanceLevel to ConformanceLevel.Fragment . 因此,只需将settings.ConformanceLevelConformanceLevel.Fragment Note that then you're technically not writing an XML document anymore, but this requirement is common in interoperability. 请注意,从技术上讲,您不再编写XML文档,但这种要求在互操作性中很常见。

I'm guessing the browser simply ignores the XML tags. 我猜测浏览器只是忽略了XML标签。 try this instead: 试试这个:

Response.Write (Server.HTMLEncode(xmlString));

Read here about the HTMLEncode method. 在这里阅读 HTMLEncode方法。

the settings.OmitXmlDeclaration = true; settings.OmitXmlDeclaration = true; should have removed the <?xml version... tag. 应该删除<?xml version...标记。 If that didn't work, you can try this: load the xmlString into an XDocument object and remove it's declaration (based on this answer) : 如果这不起作用,您可以尝试这样做:将xmlString加载到XDocument对象并删除它的声明(基于此答案)

XDocument xdoc = XDocument.Parse(xmlString);
xdoc.Declaration = null;
Response.Write (Server.HTMLEncode(xdoc.ToString()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM