【问题标题】:XDocument XDeclaration not appearing in ToString resultXDocument XDeclaration 未出现在 ToString 结果中
【发布时间】:2015-03-26 20:00:59
【问题描述】:

我正在尝试形成一个 XML 文档,我将使用该文档通过 HTTPS 将其发送到 API,但是我注意到,即使我在我的 XML 中添加了 XDeclaration 元素,XDeclaration 也不会出现在字符串中我使用xmlDoc.ToString() 方法返回。

有谁知道我是否缺少特定设置或<?xml version="1.0" encoding="UTF-8" ?> 元素没有出现的任何原因?

xmlDoc = new XDocument(
            new XDeclaration("1.0", "UTF-8", "yes"),
            new XElement("NABTransactMessage",
                new XElement("MessageInfo",
                    new XElement("MessageID", "5167813675aa47d181a7c76979f2de00"),
                    new XElement("MessageTimeStamp", "20152701024752898882+000"),
                    new XElement("timeoutValue", 60),
                    new XElement("apiVersion", "spxml-4.2")
                ),
                new XElement("MerchantInfo",
                    new XElement("MerchantID", "XYZ0010"),
                    new XElement("password", "abcd1234")
                ),
                new XElement("RequestType", "Periodic"),
                new XElement("Periodic",
                    new XElement("PeriodicList", new XAttribute("count", 1),
                        new XElement("PeriodicItem", new XAttribute("ID", 1),
                            new XElement("actionType", "addcrn"),
                            new XElement("periodicType", 5),
                            new XElement("crn", "85c2960d-1422326872"),
                            new XElement("CreditCardInfo", 
                                new XElement("cardNumber", 4111111111111111),
                                new XElement("expiryDate", "08/20"),
                                new XElement("cvv", 123)
                            )
                        )
                    )
                )
            )
        );

return xmlDoc.ToString(SaveOptions.None);

通过 HTTPS 发送请求的代码:

    public static string SendRequest(string requestContent, string requestContentType, string requestUrl)
    {
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);
            byte[] bytes;

            bytes = System.Text.Encoding.UTF8.GetBytes(requestContent);

            request.ContentType = requestContentType + "; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            //request.Timeout = 5000;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(requestContent, 0, requestContent.Length);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    return new StreamReader(responseStream).ReadToEnd();
                }
            }
        }

注意:xmlDoc.ToString() 值作为第一个参数传递给 SendRequest()requestContentType 被设置为 "text/xml"

【问题讨论】:

    标签: c# .net xml linq-to-xml


    【解决方案1】:

    XDocument.ToString() 不包括声明。而是使用XDocument.Save(),例如:

        public static string ToXml(this XDocument xDoc)
        {
            StringBuilder builder = new StringBuilder();
            using (TextWriter writer = new StringWriter(builder))
            {
                xDoc.Save(writer);
                return builder.ToString();
            }
        }
    

    如果您特别需要将编码字符串设为“UTF-8”,请参见此处:Force XDocument to write to String with UTF-8 encoding

    请注意,此扩展适用于 XDocument,而不是具有 OuterXml 的 XmlDocument。

    【讨论】:

      【解决方案2】:

      序列化为字符串不能保留 Utf-8 声明,因为它在那个时候无效 - 所以它将被删除。

      如果您需要 Utf-8(默认)编码,则需要将数据保存到流中。示例和更多讨论 - Serializing an object as UTF-8 XML in .NET

      【讨论】:

      • 我已经更新了我的问题,我不确定如何使用我当前通过 HTTP 请求发送数据的方式转换为流。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 2013-11-12
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多