XDocument XDeclaration未出现在ToString结果中 [英] XDocument XDeclaration not appearing in ToString result

查看:101
本文介绍了XDocument XDeclaration未出现在ToString结果中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to form an XML document which I will be using to send as over HTTPS to an API, however I have noticed that even though I have added an XDeclaration element to my XML the XDeclaration does not appear in the string that I return using xmlDoc.ToString() method.

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

Does anyone know if I am missing a particular setting or any reason why the <?xml version="1.0" encoding="UTF-8" ?> elements are not appearing?

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发送请求的代码:

Code to Send Request via 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"

NOTES: xmlDoc.ToString() value is being passed to SendRequest() as the first parameter, requestContentType is being set to "text/xml"

推荐答案

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

XDocument.ToString() doesn't include the declaration. Instead, use XDocument.Save(), e.g.:

    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",请参见此处:

If specifically you need to make the encoding string say "UTF-8", see here: Force XDocument to write to String with UTF-8 encoding

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

Note that this extension is for XDocument, not XmlDocument which has OuterXml.

这篇关于XDocument XDeclaration未出现在ToString结果中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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