开发者

Manually deserialize WebRequest XML

开发者 https://www.devze.com 2023-01-31 12:18 出处:网络
SO I have an ASMX web service that returns a array of Search Result objects. When I call the WebMethod via the browser, the followin开发者_如何学Cg XML is generated...

SO I have an ASMX web service that returns a array of Search Result objects. When I call the WebMethod via the browser, the followin开发者_如何学Cg XML is generated...

 <?xml version="1.0" encoding="utf-8"?><ArrayOfSearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"><SearchResult>
<Name>Rock Salt Steak House</Name>
<BusinessType>Steakhouses</BusinessType>
<Rating>mStar30</Rating>
<Address>1232 Westlake Ave N</Address>
<City>Seattle</City>
<State>WA</State>
<Phone>(206) 284-1047</Phone>
<Zip>98109</Zip></SearchResult><SearchResult>
<Name>Laredos Grill</Name>
<BusinessType>Tex-Mex Bars</BusinessType>
<Rating>mStar35</Rating>
<Address>555 Aloha St Ste 100</Address>
<City>Seattle</City>
<State>WA</State>
<Phone>(206) 218-1040</Phone>
<Zip>98109</Zip></SearchResult>.......

This XML gets saved in a file to be de-serialized later. The problem is I can't seem to get it serialized again. Here's the code I use....

    XmlSerializer serializer = new XmlSerializer(typeof(List<Service.SearchResult>));
    using (StringReader stringReader = new StringReader(strXMLContent)) // can throw ArgumentNullException
    {

        using (XmlReader xmlReader = XmlReader.Create(stringReader))
        {
            //xmlReader.Read();
            return ((List<Service.SearchResult>)serializer.Deserialize(xmlReader)).ToArray(); // can throw SerializationException

        }
    }

The error I get is complaining about "there is an error in xml document (2,2)" and the inner exception is (InnerException = {"ArrayOfSearchResult xmlns='http://tempuri.org/' was not expected."})

Of course when calling this WebMethod in code the collection comes down easily. It isn't until I try to manually deserialize later that it's get's mad.

Any ideas would be greatly appreciated...

Thanks!


First, why are you calling it via WebRequest? Why not just use "Add Service Reference" and use the proxy class?

Secondly, when you're calling it in the browser, you're not using SOAP. Note the lack of SOAP Envelope in the response.

0

精彩评论

暂无评论...
验证码 换一张
取 消