none
Creating web service API RRS feed

  • Question

  • Hi,

    This is a general question about web services. Without going into the reasons why, I'm currently creating a web service endpoint. It is written in C# and it is creating asmx web service endpoints. I know this technology is old, but there are design reasons why. All examples I find on the web are how to call a web service and post data. I'm not interested in that. The client end of this project is written and it will be doing an HTTP post and will be posting XML.

    I have a web method in my code called PostCustomer which will pick the post up and take the XML and put in into an object. I have created the class that will contain the XML. What I don't know how to do is pick up the XML from the post. The code structure is:

            [WebMethod]
            public string PostCustomer(--- not sure what parameters to take ---)
            {
                ARCustomers aRCustomers = new ARCustomers();

                if (LoginSuccessful())
                {
                    // Code here to process XML into ARCustomers();

                    return "Customer Added";
                }
                else
                {
                    return "Not logged in";
                }
            }

    I've read through lots of XML handling pages and watched videos, but not sure what I need to do here.

    • Moved by CoolDadTx Monday, June 11, 2018 1:53 PM ASP.NET related
    Monday, June 11, 2018 9:14 AM

All replies

  • XML is primitive type string so that would be the data type that would be passed into the method's signature.

    The string representation of the XML must be loaded into a XML document. So the XML response is loaded into a string variable and you take the string variable as the source to load into the XML document. 

    https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Then you can use Linq-2-XML to query the XML document doing  a Linq projection, which is  the last example in the below link, and it is using anaymous type for the collection. But you don't want to project the results of the query using an anaymous type. 

    https://www.intertech.com/Blog/query-an-xml-document-using-linq-to-xml/

    What you want to do is query the XML document using Linq, but you want to project, a projection,  the results of the query using a custom type, which is your class that you are talking about and return a collection of your custom type.

    The link shows you how to use a custom type in the Linq projection.

    https://csharp-station.com/Tutorial/Linq/Lesson02

    Other reference....

    http://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples

    https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

    ASMX and other Web services has a forum in the ASP.NET forums.

    https://forums.asp.net/

     

    Monday, June 11, 2018 10:06 AM
  • Excellent, so take a string parameter and load it into the XML doc. Can the string be loaded directly into my class? I was given an XSD doc and I used the XSD.exe in Visual Studio to generate me a class file and it has done. From what I've watched on youtube about this, once this class is generated, I pretty much don't need to do anything with it other than instantiate an object of that class type, but parsing the XML string into it seems to be a very long winded loop. Any thoughts? Any code you'd like to see of what I have?
    Monday, June 11, 2018 12:34 PM
  • Myself, I would use the XSD.exe to build the class that represents the XML elements. But after that, I would use the Linq-2-XML examples that has been shown to you where you take the XML elements, map them to the object that had a new object instance of the object created on the fly populated by the Link projection that produces a collection of your custom objects as the retuned results. 

    Let the Linq projection do the whole thing for you. All you have to figure out is how to address the elements by using the Linq-2-XML query.  

    Monday, June 11, 2018 12:53 PM
  • This question is more about getting data in a web method which is tied to ASP.NET. Please post your question in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, June 11, 2018 1:53 PM
  • Do I need to create another login on the forums.asp.net page or can I use the one I have on here? Can the question be moved to the right forum? I can't see a way of doing it.
    • Edited by MrSnert Monday, June 11, 2018 1:59 PM
    Monday, June 11, 2018 1:58 PM
  • Do I need to create another login on the forums.asp.net page or can I use the one I have on here? Can the question be moved to the right forum? I can't see a way of doing it.

    You might be able to use the one you have I think it allows you to do that.
    Monday, June 11, 2018 6:52 PM