Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

objective c - xml parsing error while using wcf framework for web services

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];

and on .net side it's likes that

[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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i called this type of WCF webservices like this try to call may be its help for you.. )

 -(void)ViewWillAppear
 {

 NSString *soapMsg = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope">"
                                 "<ToPostalCode>%@</ToPostalCode>"
                                 "<FromPostalCode>%@</FromPostalCode>"
                                 "<Weight>%@</Weight>"
                                 "<DeliveryServiceName></DeliveryServiceName>"
                                 "</SingaporePostalInfoDetailsRequest>",txtToPostalCode.text,txtFrmPostalCode.text,txtWeight.text];

            NSURL *url = [NSURL URLWithString: @http://webservice.net2survey.com/servicejson.svc/basic"];
            NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

            NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
            [req addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
            [req addValue:@"http://webservice.net2survey.com/servicejson.svc/basic" forHTTPHeaderField:@"SOAPAction"];
            [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
            [req setHTTPMethod:@"POST"];
            [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

            conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
            if (conn) {
                webData = [[NSMutableData data] retain];
            }

            objAppDelegate.arrGlbPostageSing = [[NSMutableArray alloc]init];


 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...