使用wcf框架进行Web服务时出现xml解析错误 [英] xml parsing error while using wcf framework for web services

查看:131
本文介绍了使用wcf框架进行Web服务时出现xml解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用针对iOS应用程序的wcf soap服务时,在尝试使用post方法将xml发送到服务器时遇到问题我的iOS端代码看起来像那样

it's first time while iam working on wcf soap services for iOS application , em getting problem when trying to send xml to server using post method my iOS side code looks like that

NSString *soapMessage = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SaveAllRecords33><xmlData></xmlData></SaveAllRecords33></SOAP-ENV:Body></SOAP-ENV:Envelope>"];

NSURL *url = [NSURL URLWithString:@"http://webservice.net2survey.com/servicejson.svc/SaveAllRecord33"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];                        
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];             

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"urn:ServiceJSON/SaveAllRecords33" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];    
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

在.net方面它喜欢

[WebInvoke(Method = "POST", 
           BodyStyle = WebMessageBodyStyle.WrappedRequest, 
           ResponseFormat = WebMessageFormat.Json)] 
[OperationContract] 
public string SaveAllRecords33(string xmlData) 
{ 
    //DataContractSerializer
    dataContract = new DataContractSerializer(typeof(string)); 
    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.LoadXml(xmlData);

    JavaScriptSerializer objSerialiver = new JavaScriptSerializer();
    return "{\"Answer\":" + objSerialiver.Serialize(xmlDoc.InnerXml) + "}"; 
}

当目标要点击此网址时,它会显示一些错误,如下所示

when aim going to hit this url it's showing some errors as showing bellow

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode><faultstring xml:lang="en-US">Root element is missing.</faultstring><detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>Root element is missing.</Message><StackTrace>   at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)&#xD;
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()&#xD;
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)&#xD;
   at System.Xml.XmlDocument.Load(XmlReader reader)&#xD;
   at System.Xml.XmlDocument.LoadXml(String xml)&#xD;
   at WcfService.ServiceJSON.SaveAllRecords33(String xmlData)&#xD;
   at SyncInvokeSaveAllRecords33(Object , Object[] , Object[] )&#xD;
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)&#xD;
   at System.S
2012-09-25 17:50:00.846 Net2Survey_demo[1576:b903] str: erviceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.Xml.XmlException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>


推荐答案

服务本身有效,问题不在于WCF。如果你在堆栈跟踪中向右滚动,你会看到它显示缺少根元素。并且在服务中创建XmlDocument时它会失败。

The service itself works, the issue is not in the WCF. If you scroll far right in the stack trace, you'll see that it says Root element is missing. and that it fails while creating the XmlDocument inside your service.

您只是发送一个无效的Xml字符串,但无法解析。

You are simply sending it an invalid Xml string and it fails to parse.

这篇关于使用wcf框架进行Web服务时出现xml解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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