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
LVL 2
PNRTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
0
PNRTAuthor Commented:
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
0
käµfm³d 👽Commented:
Change this line:

Dim serializer As New XmlSerializer(GetType(String))

...to this:

Dim serializer As New XmlSerializer(GetType(WindowsApplication1.Service1.MessageHeaderType))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

PNRTAuthor Commented:
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?
0
käµfm³d 👽Commented:
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?
0
PNRTAuthor Commented:
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?
0
käµfm³d 👽Commented:
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.
0
PNRTAuthor Commented:
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
0
käµfm³d 👽Commented:
Is it possible to post code related to how you "received [it] as an object?"
0
PNRTAuthor Commented:
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>
0
käµfm³d 👽Commented:
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?
0
PNRTAuthor Commented:
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
0
käµfm³d 👽Commented:
I've been slammed at work the past few days. I'll pick this up later this afternoon.
0
PNRTAuthor Commented:
Thank you Kaufmed
0
käµfm³d 👽Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.