名称不能以''字符开头,在c#中使用xml字符串的十六进制值0x20 [重复]

Posted

技术标签:

【中文标题】名称不能以\'\'字符开头,在c#中使用xml字符串的十六进制值0x20 [重复]【英文标题】:Name cannot begin with the ' ' character, hexadecimal value 0x20 in c# using xml String [duplicate]名称不能以''字符开头,在c#中使用xml字符串的十六进制值0x20 [重复] 【发布时间】:2018-08-09 16:06:57 【问题描述】:
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 字符串可能会导致问题,我无法追踪确切的错误在哪里。我在 google 中找到了一些答案,但没有找到运气 请任何人都可以帮助我,我是 C# 新手

【问题讨论】:

调试代码并找出哪行代码给出了错误 天哪,一个内联 xml 文件.. 删除标签元素名称中的所有空格。例如:`` 应该是``。 除了Evk提到的空格,encoding="UTF - 8"应该是encoding="UTF-8"没有空格。不确定其中的空间是否有效。类似于xmlns: ass1xmlns: tem 等。 @Evk 谢谢你的回答帮助了我。通过删除它工作的标签之间的空格 【参考方案1】:

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

【讨论】:

以上是关于名称不能以''字符开头,在c#中使用xml字符串的十六进制值0x20 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

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

url的非法字符都有哪些

如何检查字符串是不是是有效的 XML 元素名称?

Unity中实现读取XML文件

名称不能以 '<' 字符十六进制值 0x3c 开头

Xamarin App 编辑 info.plist 名称不能以“<”字符开头