简体   繁体   中英

Reading XML file with VB.NET

I'm building this web based app which will display weather forecasts for the next 24 hours, I'm reading an XML file with all the data I need in it already. The file however contains all the data in similar tags like this:

<response>
 <hourly_forecast>
  <forecast>
   <temp>
    <metric>DATA!</metric>
   </temp>
  </forecast>
  <forecast>
   <temp>
    <metric>MORE DATA!</metric>
   ...

As you can see, I have to enter a forecast, go through its children, find the data, and then somehow get back to the point when I can read the next forecast and its data and so on. I'm currently using XMLTextReader to be able to read, and the ReadStartElement and ReadToNextSibling methods to navigate through the file, but using those methods you cannot get back up in the file, you can only get down and in, so in order to read the next temperature I had to use a For loop that makes the program go to the next forecast 'succesfully', but it's become very resource intensive, making way too many calls to the weather API, and even giving out timeout errors. It does work if I save the XML file from the website to the project's directory on my computer, that way it's able to run fine and get all the data quick, however, I will not be able to be saving the XML files once I upload this website to the server.

So my question is, how can I extract just the data I need from this online weather XML file, and either display it or save it to a database easily and fast?

I'm working with ASP and VB on .NET Framework 3.5.

There are several ways to do this. The simplest would be to continue loading the XML the way you already are, using the XMLTextReader, but instead of only loading one at a time, load all of them at once into memory. For instance, if you created a class like this:

Public Class Forecast
    Public Property Temperature() As Integer
        Get
            Return _temperature
        End Get
        Set(ByVal value As Integer)
            _temperature = value
        End Set
    End Property
    Private _temperature As Integer

    ' ... Other properties    
End Class

Then, in your code where you load the forecasts, load all of them, instead of just one, and store them in a list like this:

Dim forecasts As New List(Of Forecast)()
' Loop through XML, and then for each forecast in XML:
    Dim f As New Forecast()
    f.Temperature = ' Set value based on current forecast data
    forecasts.Add(f)
' End loop

Then, later, when you need a specific forecast, you can just retrieve it from the already loaded list in memory:

' Get the first forecast
Dim f As Forecast = forecasts(0)

' Get the second forecast
f = forecasts(1)

' Etc.

However, I think using the XMLTextReader as you described is probably misguided. It would be easier to use XDocument , XmlDocument , or XmlSerializer . For instance, you could easily load all the forecasts into a list as I just described by using the XmlDocument class like this:

Dim forecasts As New List(Of Forecast)()
Dim doc As New XmlDocument()
doc.Load(xmlFilePath)
For Each forecastNode As XmlNode In doc.SelectNodes("/response/hourly_forecast/forecast")
    Dim f As New Forecast()
    f.Temperature = Integer.Parse(forecastNode.SelectSingleNode("temp/metric").InnerText)
Next

For such kind of the Task Make use of LINQ flavor for reading,updating, saveing and deleting data from XML.

ie Make use of LINQ to XML that will help you to achieve that you want.

There are number for source for this :

but have look to msdn over there : .NET Language-Integrated Query for XML Data

XDocument xmlDoc= XDocument.Load(@"c:sites.xml");
var q = from c in xmlDoc.Descendants("site")
select (string)c.Element("name") + " -- " +(string)c.Element("url");
foreach (string name in q) {
Console.WriteLine("Site: " + name);
}

Have you tried using a dataset.ReadXml method. This makes reading the data back very easy if you are familiar with a dataset structure. It will put your data in to datatables for you to be able to loop through.

Dim ds As New DataSet
ds.ReadXml("File Text as String")
For Each row as DataRow in ds.Tables("Forecast").Rows
  'Store your data'
Next

Hope this helps.

Re-writing @Pushpendra's example in VB using XML Literals:

Dim xmlDoc= XDocument.Load(@"c:sites.xml")
Dim query = From c in xmlDoc...<site>
            Select c.<name>.Value & " -- " & c.<url>.Value

For Each name In query
     Console.WriteLine("Site: " + name)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM