Results 1 to 16 of 16

Thread: Send XML to a web service

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Send XML to a web service

    Quote Originally Posted by MMock View Post
    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

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: Send XML to a web service

    Quote Originally Posted by gep13 View Post
    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.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Send XML to a web service

    Quote Originally Posted by MMock View Post
    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!!

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Send XML to a web service

    Quote Originally Posted by MMock View Post
    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

  8. #8

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  9. #9

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Send XML to a web service

    Hey,

    Sounds ideal!!

    Let us know how you are getting on.

    Gary

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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.
    Last edited by MMock; Feb 28th, 2011 at 07:46 AM. Reason: indentation
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  12. #12

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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!)
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  13. #13
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Send XML to a web service

    Quote Originally Posted by abhijit View Post
    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

  15. #15

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  16. #16
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Send XML to a web service

    Quote Originally Posted by MMock View Post
    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.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width