本文介绍了有XML文档中的错误(1,2),System.InvalidOperationException:或其可AuthorizationResult XLMS:"">是没有预料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML API,从发送

<AuthenticationResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<PAPIErrorCode>0</PAPIErrorCode>
<ErrorMessage/>
<AccessToken>StringAccessToken</AccessToken>
<AccessSecret>StringAccessToken</AccessSecret>
<PolarisUserID>PolarisSampleUser</PolarisUserID>
<BranchID>7</BranchID>
<AuthExpDate>2013-05-27T16:57:46.323</AuthExpDate>
</AuthenticationResult>

为响应类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace PAPIAutomatedTestingTool
{
    [XmlRoot(ElementName="AuthorizationResult")]
    public class AuthorizationResult
    {
        public int PAPIErrorCode { get; set; }

        public string ErrorMessage { get; set; }

        public string AccessToken { get; set; }

        public string AccessSecret { get; set; }

        public int PolarisUserID { get; set; }

        public int BranchID { get; set; }

        public DateTime AuthExpDate { get; set; }
    }
}

code制作要求和反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using System.Xml;
using System.Xml.Serialization;

namespace PAPIAutomatedTestingTool
{
    public class APICallMethods
    {
        public string URI { get; set; }
        public string accSecret { get; set; }
        public string accToken { get; set; }
        public string authorizationString { get; set; }

        public bool AuthenticateStaffUser()
        {
            try
            {

            //Initializing all variables
            string authReqMethod = "POST";
            string authAccessKey = "Sample Access Key";
            string authAccessKeyID = "Sample Access ID";
            string authPatronPassword = "";
            DateTime authDateTime = DateTime.Now;
            string httpAuthDateTime = authDateTime.ToUniversalTime().ToString("r");
            string authURI = "Sample URI";


            //Composing the papiHash from the given parameters
            string papiHash = GetPAPIHash(authAccessKey, authReqMethod, authURI, httpAuthDateTime, authPatronPassword);
            //Formating the authorization string
            string authorizationString = String.Format("Authorization: PWS {0}:{1}", authAccessKeyID, papiHash);


            //Creating and defining the WebRequest
            WebRequest req = WebRequest.Create(authURI);
            req.Method = "POST";
            req.Headers.Add("PolarisDate", httpAuthDateTime);
            req.Headers.Add(authorizationString);
            req.ContentType = "application/xml";
            string requestBody = "<AuthenticationData><Domain>SampleDomain</Domain><Username>SampleUsername</Username><Password>SamplePassword</Password></AuthenticationData>";
            byte[] reqBodyBytes = System.Text.Encoding.UTF8.GetBytes(requestBody);
            req.ContentLength = reqBodyBytes.Length;
            using (Stream requestStream = req.GetRequestStream())
            {
                requestStream.Write(reqBodyBytes, 0, reqBodyBytes.Length);
            }


            //Receiving the WebResponse
            using (WebResponse resp = req.GetResponse())
            {
                AuthorizationResult firstResponse = new AuthorizationResult();
                Stream respStream = resp.GetResponseStream();
                StreamReader sr = new StreamReader(respStream);


                XmlSerializer xmlSerializer = new XmlSerializer(typeof(AuthorizationResult));
                firstResponse = (AuthorizationResult)xmlSerializer.Deserialize(respStream);
                Console.WriteLine("Authorization: PWS" + firstResponse.AccessSecret + ":" + firstResponse.AccessToken);
                return true;
            }
        }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }

        }


        public string GetPAPIHash(string strAccessKey, string strHTTPMethod, string strURI, string strHTTPDate, string strPatronPassword)
        {
            byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(strAccessKey);
            HMACSHA1 hmac = new HMACSHA1(secretBytes);

            byte[] dataBytes = null;

            if (strPatronPassword.Length > 0)
            {
                dataBytes = UTF8Encoding.UTF8.GetBytes(strHTTPMethod + strURI + strHTTPDate + strPatronPassword);
            }
            else
            {
                dataBytes = UTF8Encoding.UTF8.GetBytes(strHTTPMethod + strURI + strHTTPDate);
            }
            byte[] computedHash = hmac.ComputeHash(dataBytes);
            string computedHashString = Convert.ToBase64String(computedHash);

            return computedHashString;
        }
    }
}

我想提出一个POST请求的API与包含AuthorizationData的机构。该API应该返回XML反序列化到firstresponse。我收到的XML(通过打印到控制台证实),但我收到有一个在XML文档的错误(1,2) &LT; 并没有预期; AuthorizationData的xmlns =&GT。感谢您提前帮助。

I am making a POST request to the API with a body that contains AuthorizationData. The API is supposed to return the xml to deserialize into firstresponse. I have received the xml(confirmed by printing to console) but i am receiving the There is an error in the XML Document(1,2) and <AuthorizationData xmlns=""> was not expected. Thanks for the help in advance.

推荐答案

这似乎是抱怨意想不到的根元素,并有一些混乱周围其他地方在你的问题。

It appears to be complaining about an unexpected root element and there is some confusion surrounding that elsewhere in your question.

在你有问题的标题&LT; AuthorizationResult&GT;

在你的示例XML响应&LT; AuthenticationResult&GT;

In the example XML response you have <AuthenticationResult>

在C#有 [XmlRoot(的ElementName =AuthorizationResult)]

在你的问题你提到的结束&LT; AuthorizationData&GT;

At the end of your question you mention <AuthorizationData>

这是一个主题了不少微妙的变化。

That's quite a few subtle variations on a theme.

假设XML样本响应是正确的,你应该改变C#期望根元素是&LT; AuthenticationResult&GT; ...

Assuming the XML sample response is correct, you should change the C# to expect the root element to be <AuthenticationResult>...

[XmlRoot(ElementName="AuthenticationResult")]
public class AuthenticationResult
{
    ...

这篇关于有XML文档中的错误(1,2),System.InvalidOperationException:或其可AuthorizationResult XLMS:&QUOT;&QUOT;&GT;是没有预料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:08