簡體   English   中英

后XML“根元素丟失”

[英]Post XML “Root Element Is Missing”

發布XML數據時,我一直收到臭名昭著的“缺少根元素”錯誤。 有時它可以工作,但大多數情況下會引發錯誤。

C#代碼:

 public string postXMLData(string destinationUrl, string requestXml)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                return responseStr;
            }
            return "";
        }

XML數據:

<Person>
    <idPeople>0</idPeople>
    <AccountNumber>TEST02H</AccountNumber>
    <cFirstName>TEST 2</cFirstName>
    <cLastName>TEST 2</cLastName>
    <cTelWork/>
    <cTelMobile>0713473835</cTelMobile>
    <cEmail>myemail@mydomain.com</cEmail>
    <ubPPLIsActive>true</ubPPLIsActive>
    <ubPPLSMSOptIn>true</ubPPLSMSOptIn>
    <udPPLSMSOptInActivateDate>30/05/2017</udPPLSMSOptInActivateDate>
    <ulPPLAccessType>Administrator</ulPPLAccessType>
    <ubPPLNewsletterSignUp>true</ubPPLNewsletterSignUp>
    <ubPPLIncludeBuyerEmails>true</ubPPLIncludeBuyerEmails>
</Person>

對我來說,問題在2個地方。

  1. 我要發布的應用程序出現錯誤,我已解決。 我通過將目標URL粘貼到瀏覽器中來查看發生了什么,從而發現了此錯誤。

  2. 我需要System.Web.HttpUtility.UrlEncode XML。

最后我使用了這種方法。

WebClient client = new WebClient();
string xmlResult = client.DownloadString(_workContext.AccountingWebServiceLink + "?action=updatesecondary&xml=" + System.Web.HttpUtility.UrlEncode(strXML));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM