Send XML to a web service
That is what I have to do: Send XML to a web service. I have preliminary requirements of what I have to generate in the XML. So, I just create this big, long XML stream and send it to the service? I would like to read a little bit about this to educate myself. Do you have a good link, or can you voice your opinion of this one
Additionally, I am coding in VB.NET so I will have to convert that code if it is a good example.
Thanks.
Re: Send XML to a web service
Once you add the webservice as reference to your project, you can use any .Net language to create a class out of this.
From that point onwards, you can create and instantiate the class.
You do not need to form the XML to make a web-service call.
Where is this webservice you're calling?
Re: Send XML to a web service
Quote:
Originally Posted by
MMock
Additionally, I am coding in VB.NET so I will have to convert that code if it is a good example.
Well there is your first problem, you should move away from the dark side, into the light that is C# :)
All joking aside...
I don't think that MMock was talking about looking at the XML of the WSDL for the WebService or anything like that, but rather, a Web Service Method is expecting an XML fragment as an input parameter to the method. This is perfectly valid, and used on a number of Web Services.
MMock, can you show the "contract" for the WebMethod that you are trying to call? i.e. the parameters that are expected? Typically, when I have had to do this, the proxy class that gets generated for the WebService method requires an XElement, or an XmlElement as a pattern, so what that means is that I simply create the required snippet in my code, and then pass that as a parameter to the WebMethod.
Gary
Re: Send XML to a web service
Well, this is all I know/don't know about the project so far. I do not have a contract, the name of the service or anything like that yet. What I have is some example XML of what the client is going to want. The code I have written so far places an order online, and the client of the website wants information about the order. E.g.
Code:
<ShipTo>
<Address>
<Name xml:lang="en">Mockware</Name>
<PostalAddress name="default">
<DeliverTo>MMock</DeliverTo>
<Street>123 Anystreet</Street>
<City>Sunnyvale</City>
<State>CA</State>
<PostalCode>90489</PostalCode>
<Country isoCountryCode="US">United States
</Country>
</PostalAddress>
</Address>
</ShipTo>
So I thought I at least had enough info to begin, though of course I won't be calling the service yet. But I can get a good start on generating this code, and plugging in all the variables (Street, City, State, etc).
I just am not sure of the best way to begin creating the XML. Of course I want to do it in such a way that when I have the interface to the service, I can just let it rip.
Thanks.
Re: Send XML to a web service
Quote:
Originally Posted by
gep13
Well there is your first problem, you should move away from the dark side, into the light that is C# :)
We actually may be. The developer who had a great stake in VB.NET is no longer here and I overheard management mention that we can become a mixed shop of C# and VB.NET now. I took many wrong turns way up the road and VB.NET jobs are all I've ever had.
Re: Send XML to a web service
Quote:
Originally Posted by
MMock
We actually may be. The developer who had a great stake in VB.NET is no longer here and I overheard management mention that we can become a mixed shop of C# and VB.NET now. I took many wrong turns way up the road and VB.NET jobs are all I've ever had.
Woot woot!! :)
Re: Send XML to a web service
Quote:
Originally Posted by
MMock
Well, this is all I know/don't know about the project so far. I do not have a contract, the name of the service or anything like that yet. What I have is some example XML of what the client is going to want. The code I have written so far places an order online, and the client of the website wants information about the order. E.g.
Code:
<ShipTo>
<Address>
<Name xml:lang="en">Mockware</Name>
<PostalAddress name="default">
<DeliverTo>MMock</DeliverTo>
<Street>123 Anystreet</Street>
<City>Sunnyvale</City>
<State>CA</State>
<PostalCode>90489</PostalCode>
<Country isoCountryCode="US">United States
</Country>
</PostalAddress>
</Address>
</ShipTo>
So I thought I at least had enough info to begin, though of course I won't be calling the service yet. But I can get a good start on generating this code, and plugging in all the variables (Street, City, State, etc).
I just am not sure of the best way to begin creating the XML. Of course I want to do it in such a way that when I have the interface to the service, I can just let it rip.
Thanks.
So, are you saying that you don't actually have a Web Service stub that you can create a Web Service Reference to? Do you have the WSDL file for the Web Service? If you at least had that, you could create the proxy classes for the Web Service.
To create the actual XML, you could just create an XmlDocument class, and start working from there. Or you could use LINQ. Whatever takes your fancy.
Although, it would be good to know the exact contract for the WebService, so that you could guard against problems once you actually start hitting it.
Gary
Re: Send XML to a web service
I've only worked with one web service before. I wrote it. I think I was also the consumer. I can't remember much about it, so all your questions are things I didn't even know I should ask. But that's what I want to know - can I just start creating the XML since that is a known piece of information, or I don't know enough yet to even start on this? We have daily meetings at 9:15 - I can ask questions then. In the meantime, I guess I will start at the beginning and research web services. Is a proxy class always how you communicate with a web service? But even without this, can't I just create the XML? For example, I could write it out or look at it in the debugger and make sure I have the correct format according to the specs?
Re: Send XML to a web service
Good news! The web service doesn't even exist yet! One of our developers is going to be writing it. So it will be nice to be able to work directly with the author. Also, I can probably tell her what I am giving her, and she will write the service to accomodate my input!
Re: Send XML to a web service
Hey,
Sounds ideal!!
Let us know how you are getting on.
Gary
Re: Send XML to a web service
The code I posted in #4, specifically
Code:
<Name xml:lang="en">Mockware</Name>
is that invalid syntax? I am getting an exception The ' ' character, hexadecimal value 0x20, cannot be included in a name when I try to execute:
Code:
sb.Append("Name xml:lang=")
sb.Append("""")
sb.Append("en")
sb.Append("""")
Try
Dim name As XmlElement = oXmlDoc.CreateElement(sb.ToString)
name.InnerText = "Mockware"
address.AppendChild(name)
Catch ex As Exception
Dim s As String
s = ex.Message
End Try
Thank you.
Re: Send XML to a web service
I am going to open a new thread about my last question as I think this is very general but buried in a more specific topic so I think it's getting overlooked (or maybe I am just impatient!)
Re: Send XML to a web service
One thing I noticed is that you're using a string builder to concatenate the XML together. The easier approach would be to use XML libraries in .NET to achieve the same goal.
Re: Send XML to a web service
Quote:
Originally Posted by
abhijit
One thing I noticed is that you're using a string builder to concatenate the XML together. The easier approach would be to use XML libraries in .NET to achieve the same goal.
I would have to agree with this. The XmlDocument class has a number of methods built in that can help with adding child elements, and attributes etc.
Gary
Re: Send XML to a web service
Right - and I am using them - what specifically should I be doing to build the string? The reason I went with a stringbuilder is because when I use one it is very clear to me what I am doing with double quotation marks within strings, like
sb.Append("""")
sb.Append("default")
sb.Append("""")
Dim postalAddress As XmlElement = oXmlDoc.CreateElement(sb.ToString)
but let me know if there's something even better!
Re: Send XML to a web service
Quote:
Originally Posted by
MMock
Right - and I am using them - what specifically should I be doing to build the string? The reason I went with a stringbuilder is because when I use one it is very clear to me what I am doing with double quotation marks within strings, like
sb.Append("""")
sb.Append("default")
sb.Append("""")
Dim postalAddress As XmlElement = oXmlDoc.CreateElement(sb.ToString)
but let me know if there's something even better!
There are several tutorials on the internet, containing sample code.
Here's one.