How to read xmlString passed via SOAP to Web Service ASMX

I am trying to send a SOAP message (which contains my xmlString) to my Web Service.
The Web Service needs to read my xmlString, do something, then return something.
I am having problems getting my Web Service ASMX to read the SOAP xmlString.


This is my Web Service info

POST /myWebService.asmx HTTP/1.1
Host:   mywebsite.com()
Content-Type: text/xml; charset=utf-8
        Content(-Length) : length()
SOAPAction: http://mywebsite.com/Pets

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Pets xmlns="http://website.com/">
      <xmlString>string</xmlString>
    </Pets>
  </soap:Body>
</soap:Envelope>




This is the SOAP Xml that I am sending to the Web Service

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
   <Pets xmlns="http://mywebsite.com/">
        <?xml version="1.0" encoding="utf-8"?>
        <myPet xmlns="http://mywebsite.com/">
       <Animal>Dog</Animal>
       <Breed>Shepard</Breed>
       <Name>Rex</Name>
       <myPet>
   </Pets>
</soap:Body>
</soap:Envelope>






This is my Web Service ASMX info

<WebMethod()>_
Public Function Pets(ByVal xmlString As String) As String

    ??? How do I read the passed xmlString & parse our the values Animal, Breed, Name, etc ???

    ' I thought this might work....
    Dim sr as New StringReader(xmlString)
    Dim xmlReader as New XmlTextReader(sr)
    sr.Close()
    sr=nothing

    Dim curAnimal as string = ""
    xmlReader.WhitespaceHandling = WhitespaceHandlinig.None
    While xmlReader.Read()
         if xmlReader.Name = "Animal" then
               curAnimal = xmlReader.ReadElementString("Animal")
         end if
     xmlReader.Close()
     xmlReader=nothing

    Dim strXMLStringResponse as string = "ok"
    Return strXMLStringResponse
End Function



Error: I am getting this error --> Server Error in '/' Application

I believe that the ASMX Web Service is not understanding the xmlString that I passed in my SOAP message

Any ideas ??


















MikeCombeAsked:
Who is Participating?
 
anuguCommented:
Hi,

You can create a class named Pet like:

public class Pet
{

   Public Pet()

   public String Name { get; set; }
   public String Animal { get; set; }
   public String Breed { get; set; }
}

And than use XmlSerializer to create an object from the string like:

XmlSerializer ser = new XmlSerializer(typeof(Pet))

ser.Deserialize( ....... ) use an xmlreader and a stringbuilder here.


See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx for more info about the XmlSerializer...

Note: the class must have a parameterless constructor and all the properties must be public and settable...

You can also do this with a list of pets etc....



0
 
MikeCombeAuthor Commented:
no....that is the wrong direction.
I want to pass an xmlString via SOAP to my Web Service.
I want the Web Service to parse the xmlString, then return an xmlStringReponse.

0
 
anuguCommented:
With the code above you can 'parse' your xmlString into an object (Deserialize it) in your webservice. That was an answer to your question " ??? How do I read the passed xmlString & parse our the values Animal, Breed, Name, etc ???"

That way you will be able to do things like :

if (pet.Animal == "Dog")
{

}

As for your exception, the xmlString tag in the delivered soap message is missing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.