【问题标题】:.Net Core XmlDocument doesn't return declaration.Net Core XmlDocument 不返回声明
【发布时间】:2019-01-09 17:42:56
【问题描述】:

我有一个带有 XmlDocument 的 .net 核心 Web 应用程序,即使没有修改,它也不会返回声明。

我有这个代码

[HttpPost]
[Consumes("application/xml")]
[Produces("application/xml")]
public ActionResult<XmlDocument> GW1()
{
    XmlDocument xmlDocRec = new XmlDocument();
    xmlDocRec.Load(Request.Body);
    return Ok(xmlDocRec);
}

请求

<?xml version="1.0" encoding="utf-8"?>
<GR User="User1" PropertyCode="90001045">
    <GW>1</GW>
</GR>

回应

<GR User="User1" PropertyCode="90001045">
    <GW>1</GW>
</GR>

我在启动中有这个

services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddXmlSerializerFormatters();

我需要回复&lt;?xml version="1.0" encoding="utf-8"?&gt;,但我不知道为什么它不返回。在 xmlDocRec.InnerXml 和 xmlDocRec.OuterXml 中存在。

我没有作为参数和响应的类,我不能将它用于需求,因为我使用 Request.Body

显然我使用 xmlDocRec,我添加和更新元素,但结果是相同的。当我使用 xmlDocRec 时,xmlDocRec.InnerXml 和 xmlDocRec.OuterXml 包含&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;。稍后,我将需要删除standalone="no",因为它不能响应。

--- 编辑

我不知道这是否是正确的方式,但现在我正在使用这个

[HttpPost]
public ContentResult GW1()
{
    XmlDocument xmlDocRec = new XmlDocument();
    xmlDocRec.Load(Request.Body);

    return new ContentResult
    {
        ContentType = "application/xml",
        Content = xmlDocRec.OuterXml,
        StatusCode = 200
    };
}

这样,我就不需要在启动时使用、生产和 AddXmlSerializerFormatters。

如果有人知道更好的方法,我愿意尝试。

【问题讨论】:

  • 是否需要使用XmlDocument? XDocument 呢?
  • 是的,我需要使用 XmlDocument

标签: c# xml asp.net-core .net-core xmldocument


【解决方案1】:

您可以尝试使用 XmlWriterSettings.OmitXmlDeclaration 在 ConfigureServices 方法中配置您的 XmlSerializerOutputFormatter,并将其设置为 false,因为默认情况下设置为 true。

private void ConfigureXmlOutpuFormatter(MvcOptions options)
{
     XmlWriterSettings settings = new XmlWriterSettings
     {
       OmitXmlDeclaration = false
     };
     options.OutputFormatters.Add(new XmlSerializerOutputFormatter(settings));
}

【讨论】:

    【解决方案2】:

    您可能希望使用在 XmlDeclaration 类上设置的显式属性值来制作您的响应。

    我建议你看看XmlDeclaration Class

    [HttpPost]
    [Consumes("application/xml")]
    [Produces("application/xml")]
    public ActionResult<XmlDocument> EchoXmlAndChangeEncoding()
    {
        string requestXML = Request.Body;
    
        XmlDocument doc = new XmlDocument();
        doc.Load(new StringReader(requestXML));
    
        // Grab the XML declaration. 
        XmlDeclaration xmldecl = doc.ChildNodes.OfType<XmlDeclaration>().FirstOrDefault();
        xmldecl.Encoding = "UTF-8";
        xmldecl.Standalone = null;   // <-- or do whatever you need
    
        ... // set other declarations here
    
        // Output the modified XML document 
        return Ok(doc.OuterXml);
    }
    

    【讨论】:

    • 我读过那篇文章,但它需要 ´XmlElement root = doc.DocumentElement; doc.InsertBefore(xmldecl, root);´ 但会引发错误,无法在指定位置插入节点。因为退出,InnerXml 和 OuterXml 显示它但 return 没有
    • @M.Mis -- 鉴于您需要一个更明确的答案(而不是提示),我已更新我的答案以向您展示整个方法。
    • 谢谢,但没有任何改变,即使是字符串 requestXML = Request.Body 也会抛出错误。 doc.OuterXml 返回一个带有声明的字符串,而不是一个 xml,但是,我原来的问题是怎么说的,我的带有 OuterXml 的代码也返回声明。我想,我的问题在于配置,但我不知道我想念什么
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2012-12-21
    • 2019-03-31
    • 2021-05-26
    • 2018-10-13
    • 2011-09-30
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多