Link to home
Start Free TrialLog in
Avatar of PNRT
PNRT

asked on

VB.Net - Convert obj into data or xml

Hi Experts. I have downloaded data from a web service which is now an object in the code but which originally was an xml file.  Please could someone indicate how I extract the data from the object either as variables or as an xml file.   Variables would be preferred.    Thanks in advance
Avatar of kaufmed
kaufmed
Flag of United States of America image

Avatar of PNRT
PNRT

ASKER

Hi, many thanks for the reply.  Great article and looked exactly what I needed but when I changed the example from data to my object - obj ......

Dim filename As String = "test.dat"
Dim serializer As New XmlSerializer(GetType(String))
        Using writer As New XmlTextWriter(filename, Encoding.UTF8)
            Try
                serializer.Serialize(writer, obj)
                Console.WriteLine("Serialization successful! Wrote test.dat!")
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
        End Using

I got this error

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Unable to cast object of type 'WindowsApplication1.Service1.MessageHeaderType' to type 'System.String'.

If it makes a difference, I forgot to mention that this is a soap server?

Any idea what I'm doing wrong?  Thanks again
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PNRT

ASKER

SO impressive - many thanks, worked perfectly!  Just as a point, I must now read the xml data into a connection string.  Is there maybe a way of pulling the data directly in to variables instead of having to now get them from the newly created xml file?
Is there maybe a way of pulling the data directly in to variables instead of having to now get them from the newly created xml file?
I don't understand the question. If they do not come from the file, where will they be coming from?
Avatar of PNRT

ASKER

Directly from the object itself without creating the xml file first?
So instead of converting the object into an xml file and then getting the values from the xml file, just getting the values directly from the object.   So really trying to cut out one of the steps?    Is that feasible?
If you already have the data in an instance of a class or structure, then just access any of the instance's public methods or properties. There is no need to convert to XML to extract the data.
Avatar of PNRT

ASKER

Sorry to be stupid, but that's what I was asking.   If I have the data in an object, how do I access it.   The file received was originally sent as an xml file containing the data and received as an object.    I really only need the data from the object but don't know how to access it.  Nothing in my searches seems to address accessing the individual data in the object.   Many thanks for replying
Is it possible to post code related to how you "received [it] as an object?"
Avatar of PNRT

ASKER

Hi - This is the code.  Many thanks for coming back

Dim wGLN As New wService1.GetMessage With {.Number = "2456276", .MsgType = wService1.MessageType.Download, .acceptAll = wService1.yesNo.Y}

From your example it produces this xml

<?xml version="1.0" encoding="UTF-8"?>
<MessageType creationDate="2013-09-04T17:46:36.816" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <user xmlns="https://www.xxxxx..com/download">fred</user>
      <Number xmlns="https://www.xxxxx..com/dloads">2456276</Number>
      <to xmlns="https://www.xxxxx.com/dloads">
      <IDNumber>57644350</IDNumber>
      </to>
</MessageType>
Hmmm...  Does the GetMessage class actually go and retrieve the data? Do you have to call any method on that class to initiate the download?
Avatar of PNRT

ASKER

Hi, yes.  This is the function

Dim inValue As wService1.GetMessage = New wService1.GetMessage()
inValue.Security = Security
inValue.GetMessage = GetMessage
Dim retVal As wService1.GetMessage = CType(Me,wService1.IPullService).GetMessage(inValue)
Return retVal.GetMessage
I've been slammed at work the past few days. I'll pick this up later this afternoon.
Avatar of PNRT

ASKER

Thank you Kaufmed
OK, so in that last example you have two objects that have some data:  You have the parameter going to the service, and you have the return from the service. This is inValue and retVal, respectively. Each of those are instances of classes that should have some properties available to you. I assume you are interested in the return from the service, so inspect the properties of retVal to see if they are the ones you are after.

.NET will take care of transforming your parameter into the XML that will go out on the wire; likewise, it will take care of converting the XML coming in on the wire for the response into the object that you have for the return value.