Home > Blockchain >  Capture incoming XML from API post in VB.net
Capture incoming XML from API post in VB.net

Time:10-19

I'm trying to work out how to capture the XML POSTed to a VB.NET Rest API in VS 2019

I've created a new ASP.NET web application project, created it Empty but checked the 'Add folders & core references' Web API option.

After loading.. I added a new class library (VB)

I added an ADO.net entity data model to the class just created and connected it to my database procedure

I added a reference to my web api projects pointing it to the my new data access project

Finally i added a new 'WEB api2 controller - Empty' to the web api project

In the controller I see the following code

Namespace Controllers
    Public Class EmployeesController
        Inherits ApiController

        ' *what code goes in here?*

    End Class
End Namespace

My question is how do I read the incoming xml at this point?

I am using POSTMAN to post the following test XML, keeping it really simple

<stock>
    <stock_code>abc1234</stock_code>
</stock>

I can do it in C# and am able to process the XML Ok. I've been pulling my hair out with this but just cant fathom out how to do it in VB.NET, any help greatly appreciated.

Ok so, in C# I would be doing something like this:

namespace MyApp.Controllers
{

public class MyController : ApiController
{
    public string ReturnXmlDocument(HttpRequestMessage request)
    {
        var doc = new XmlDocument();
        doc.Load(request.Content.ReadAsStreamAsync().Result);

        String Mystr;
        Mystr = GetXMLAsString(doc);

do other stuff....
    }
}

CodePudding user response:

Well who knew this website which converts C# code to VB existed?

https://converter.telerik.com/

I quick bit of cut & pasting and I now have my C# code is converted to VB which works a treat. the Vb code is below:

Public Class MyController
    Inherits ApiController

    Public Function ReturnXmlDocument(ByVal request As HttpRequestMessage) As String
        Dim doc = New XmlDocument()
        doc.Load(request.Content.ReadAsStreamAsync().Result)
        Dim Mystr As String
        Mystr = GetXMLAsString(doc)

        Do
            Dim stuff As other
        Loop While _
    End Function
End Class
  • Related