Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:40:54+00:00 2026-06-12T12:40:54+00:00

I’m working on a project in which I have to save data I read

  • 0

I’m working on a project in which I have to save data I read from an XML file to a database, I’m already using code I made but it’s a bit primitive, I’m using XMLTextReader and a few other methods to navigate through the lines of code on the file and read the data I want, however I’ve read this would be so much “easier” if I used Serialization on my program, now I’ve seen a few examples of this and to be honest haven’t quite understood them, never even heard of them before, so if anyone could explain it to me in general terms and provide a little example on how I could apply it to my project it would be great.

This is what I have now:

For y As Integer = 0 To 167
            Dim reader1 As XmlTextReader = New XmlTextReader(dir)

            reader1.ReadStartElement("response")
            reader1.ReadToNextSibling("hourly_forecast")
            reader1.ReadStartElement("hourly_forecast")
            reader1.ReadToNextSibling("forecast")
            reader1.ReadStartElement("forecast")

            CurrentLine= reader1.LineNumber
            If CurrentLine= LastLine Then
                reader1.ReadToNextSibling("forecast")
                CurrentLine= reader1.LineNumber
            End If
            While reader1.LineNumber <= LastLine
                For w As Integer = 0 To y
                    reader1.ReadToNextSibling("forecast")
                    CurrentLine= reader1.LineNumber
                Next w
            End While

            LastLine = CurrentLine
            For x As Integer = 1 To 2
                Dim reader As XmlTextReader = New XmlTextReader(dir)

                reader.ReadStartElement("response")
                reader.ReadToNextSibling("hourly_forecast")
                reader.ReadStartElement("hourly_forecast")
                reader.ReadToNextSibling("forecast")
                Do While reader.LineNumber < CurrentLine
                    reader.ReadToNextSibling("forecast")
                Loop
                reader.ReadStartElement("forecast")

                If x = 1 Then
                    reader.ReadToNextSibling("FCTTIME")
                    reader.ReadStartElement("FCTTIME")
                    reader.ReadToNextSibling("hour")
                    values(y, x) = reader.ReadString()
                    d = values(y, x)
                    reader.ReadToNextSibling("year")
                    year = lector.ReadString()
                    reader.ReadToNextSibling("mon_padded")
                    month = reader.ReadString()
                    reader.ReadToNextSibling("mday_padded")
                    day= reader.ReadString()
                    a = year.Chars(2)
                    b = year.Chars(3)
                    c = a + b
                    x = x - 1
                    values(y, x) = day + "-" + month + "-" + c
                    x = x + 1
                End If
                If x = 2 Then
                    reader.ReadToNextSibling("temp")
                    reader.ReadStartElement("temp")
                    reader.ReadToNextSibling("metric")
                    values(y, x) = reader.ReadString()
                End If

            Next x

        Next y

This is an example of what my XML looks like:

 <response>
  <version>0.1</version> 
  <features>
   <feature>hourly10day</feature> 
  </features>
  <hourly_forecast>
   <forecast>
    <FCTTIME>
     <hour>16</hour> 
     <year>2012</year> 
     <mon_padded>10</mon_padded> 
     <mday_padded>05</mday_padded> 
    </FCTTIME>
    <temp>
     <english>102</english> 
     <metric>39</metric> 
    </temp>
   </forecast>
  </hourly_forecast>
 </response>

Now, this works, but how could I apply the deserialization to it? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:40:55+00:00Added an answer on June 12, 2026 at 12:40 pm

    Serialization is “easy” in the sense that you can convert XML into an object in a few lines of code. But depending on the complexity of the XML and/or object, there may be some setup you have to do (by way of adding attributes to the class definition) in order to have serialization be successful.

    For example, with your XML a bit of decoration is warranted, I think. Here’s the beginnings of a set of classes which could represent your sample XML:

    <XmlRoot("response")>
    Public Class Response
        <XmlElement("version")>
        Public Property Version As String
        <XmlArrayItem("feature")>
        <XmlArray("features")>
        Public Property Features As List(Of String)
        <XmlElement("hourly_forecast")>
        Public Property HourlyForecast As HourlyForecast
    End Class
    
    Public Class HourlyForecast
        <XmlElement("forecast")>
        Public Property Forecast As Forecast
    End Class
    
    Public Class Forecast
        <XmlElement("FCTTIME")>
        Public Property FctTime As FctTime
    End Class
    
    Public Class FctTime
        <XmlElement("hour")>
        Public Property Hour As String
        <XmlElement("year")>
        Public Property Year As String
        <XmlElement("mon_padded")>
        Public Property PaddedMonth As String
        <XmlElement("mday_padded")>
        Public Property PaddedMonthDay As String
    End Class
    

    You will notice that many of the properties and a one of the classes are decorated with attributes. Theses attributes help the serializer coordinate the placement of each value within the XML into the appropriate property within the object. Ordinarily, one would name the classes and properties the same as what is found in XML. In doing so, you can often get away without having to use attributes. In this case, because .NET practice is to use Pascal casing for class and property names, and because your XML is using lowercase, I inserted the attributes to correctly associate each XML node with the corresponding property/class. Without doing so, the serializer would not be able to correctly map the elements to the object.

    I mentioned that moving from the XML to the object in code could be accomplished in a few lines. Here is what that might look like:

    Imports System.Xml.Serialization
    Imports System.IO
    
    Module Module1
    
        Sub Main()
            Dim serializer As New XmlSerializer(GetType(Response))
            Dim reader As New StreamReader("dump.xml")
            Dim instance As Response = serializer.Deserialize(reader)
    
            reader.Dispose()
        End Sub
    
    End Module
    

    While the code to construct the class which represents the XML admittedly does take a few lines, this is unavoidable. You would have something similar in your approach–less the attributes, of course. The savings here is that you convert the object in one swift call to the Serialize method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.