Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deserialize XML String 1

Status
Not open for further replies.

deulyd

Programmer
Oct 17, 2001
106
CA
Hi,

I have an XML array stored into a string variable. I juste need to deserialize it to retrieve the array. I tried the System.Xml.Serialization.XmlSerializer object to Deserialize it but that method only takes stream, textReader or XMLReader; no string! :(

Please help

Thanks

Daniel
 
How was the XML created?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
It's created by an external application that I can't manage. Here's my XML String I need to deserialize :

<?xml version="1.0" ?>
- <complexType length="5">
- <complexType length="2">
<string xml:space="preserve">Daniel</string>
<string xml:space="preserve">Bordeleau</string>
</complexType>
- <complexType length="2">
<string xml:space="preserve">Robert</string>
<string xml:space="preserve">Payant</string>
</complexType>
- <complexType length="2">
<string xml:space="preserve">Helene</string>
<string xml:space="preserve">Bock</string>
</complexType>
- <complexType length="2">
<string xml:space="preserve">Martin</string>
<string xml:space="preserve">Roy</string>
</complexType>
- <complexType length="2">
<string xml:space="preserve">Marlene</string>
<string xml:space="preserve">Duval</string>
</complexType>
</complexType>
 
OK, technically, this isn't deserialization. You've got a series of records in an xml string that you want to turn into a collection of objects.

Since the structure seems pretty well ordered (even if the element names sure look like they're from an XSD), I would open an XMLTextReader and start reading the file. When you get to the level where's there's data (your 2nd "complexType" element), I would pass the reader to a subroutine which would read the XML, using reader.GetString, etc, to create a person object. This object then gets added to an ArrayList. Continue to end of file.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks!

But would you give me an example on how to do that, I'm really new to the .NET world. Also is it possible to read it from a string instead of a file, because My XML is stored in a string.

Daniel
 
Ok, I've managed to find a way to make the XMLTextReader work and I can now read the data. But within parameters of the XMLTextReader I can get the values and the Elements. But how can I get the lenght of the complex type elements?

Thanks

Daniel
 
Are you sure you need the length? It looks like it's 2 every time.

You can do something like:
Code:
If reader.HasAttributes Then
  Dim i As Integer
  For i = 0 To reader.AttributeCount - 1
     Console.WriteLine(reader(i).ToString())
  Next i
End If

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top