名称不能以c#中的''字符,十六进制值0x20开头,使用xml String

时间:2018-03-01 14:48:09

标签: c# xml web-services

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Net;
using System.IO;
namespace SOAPServices
{
    class Program
    {
        static void Main(string[] args)
        {
            CallWebService();
        }


public static void CallWebService()
    {
        var _url = "http://exampleurl.svc";
        var _action = "http://examplemethod";

        XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
        HttpWebRequest webRequest = CreateWebRequest(_url, _action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

        // begin async call to web request.
        IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

        // suspend this thread until call is complete. You might want to
        // do something usefull here like update your UI.
        asyncResult.AsyncWaitHandle.WaitOne();

        // get the response from the completed web request.
        string soapResult;
        using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
        {
            using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
            {
                soapResult = rd.ReadToEnd();
            }
            Console.Write(soapResult);
        }
    }

    private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static XmlDocument CreateSoapEnvelope()
    {
        XmlDocument soapEnvelopeDocument = new XmlDocument();
        soapEnvelopeDocument.LoadXml(@"<?xml version=""1.0"" encoding=""UTF - 8""?>
< soapenv:Envelope xmlns:soapenv = ""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ass = ""http://assetrecovery.assurant.com/"" xmlns: ass1 = ""http://schemas.datacontract.org/2004/07/AssetRecovery_Service.Entities"" xmlns: tem = ""http://tempuri.org/"" >
   < soapenv:Header >
      < ass:AuthenticationHeader >
         < !--Optional:-->
         < ass:Password > Testing </ ass:Password >
         < !--Optional:-->
         < ass:UserName > Testing </ ass:UserName >
         < !--Optional:-->
         < ass:VersionNumber > 3 </ ass:VersionNumber >
      </ ass:AuthenticationHeader >
   </ soapenv:Header >
   < soapenv:Body >
      < tem:Set_SubmitClaim >
         < !--Optional:-->
         < tem:request >
            < ass1:AccountID > 987605589 </ ass1:AccountID >
            < ass1:City > WAYNE </ ass1:City >
            < ass1:ClaimantContactNumber > 9329496466 </ ass1:ClaimantContactNumber >
            < ass1:ClaimantFirstName > BILL </ ass1:ClaimantFirstName >
            < ass1:ClaimantLastName > SMITH </ ass1:ClaimantLastName >
            < ass1:DateOfLoss > 02 / 20 / 2018 </ ass1:DateOfLoss >
            < !--Optional:-->
            < ass1:EmailAddress > test@test.com </ ass1:EmailAddress >
            < ass1:InsuredFirstName > BILL </ ass1:InsuredFirstName >
            < ass1:InsuredLastName > SMITH </ ass1:InsuredLastName >
            < ass1:LossDescription > DROPPED IN THE MALL TEST </ ass1:LossDescription >
            < ass1:LossType > PHYSICAL_DAMAGE </ ass1:LossType >
            < ass1:Manufacturer > APPLE </ ass1:Manufacturer >
            < ass1:Model > IPHONE 6 PLUS 16GB SPACE GRAY TMO </ ass1:Model >
            < !--Optional:-->
            < ass1:PhoneProblem > CRACKET SCREEN / DISPLAY </ ass1:PhoneProblem >
            < ass1:Program > JUMP </ ass1:Program >
            < ass1:RepID > 7578 </ ass1:RepID >
            < ass1:SKU > MGAX2LL / A </ ass1:SKU >
            < ass1:SerialNumber > 105589918105590 </ ass1:SerialNumber >
            < ass1:State > PA </ ass1:State >
            < ass1:StreetAddress1 > 676 E SWEDESFORD RD </ ass1:StreetAddress1 >
            < !--Optional:-->
            < ass1:StreetAddress2 />
            < ass1:SubscriberID > 8080805589 </ ass1:SubscriberID >
            < ass1:Zip > 19087 </ ass1:Zip >
         </ tem:request >
      </ tem:Set_SubmitClaim >
   </ soapenv:Body >
</ soapenv:Envelope > ");
        return soapEnvelopeDocument;
        }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }
}
}

通过执行上面的代码,我得到的xml错误就像名称不能以''字符,十六进制值0x20开头。 xml字符串可能导致问题,我无法追踪确切的错误在哪里。我在谷歌找到了一些答案,但没有找到运气 请任何人帮助我,我是C#

的新手

1 个答案:

答案 0 :(得分:2)

正如您对问题的评论所指出的,错误消息的含义正是它所说的:在开始或结束标记中的名称之前不允许使用空格。