Solved

Parsing XML with asp.net

Posted on 2015-01-09
4
351 Views
Last Modified: 2016-02-17
Hello Experts,
I am not a programmer and I've spent enough time trying to make it work. I have an XML file with weather data from AWS and am trying to get some data from it. As you can see from the attachment, the <entry> item can appear multiple times thus I need to account for it to be displayed. I am interested in <title> and <summary> nodes at the moment.

I could read it to a string and then fiddle with the string but I wanted to make it right :)

Any help appreciated.



Thanks,
Marek
WX-feed.xml
0
Comment
Question by:maredzki
  • 2
4 Comments
 
LVL 2

Author Comment

by:maredzki
ID: 40540570
I have this code snippet but it is not what I am looking for as it displays the whole <entry> branch, and not accounting for multiple. Today there are two alerts for that link listed below, there might not be in the future when the alert expires.

Dim document As New XPathDocument("http://alerts.weather.gov/cap/wwaatmget.php?x=MIC021&y=1")
        Dim navigator As XPathNavigator = document.CreateNavigator()
        Dim nodes As XPathNodeIterator = navigator.[Select]("*[local-name()='feed']/*[local-name() = 'entry']")

        nodes.MoveNext()
        Dim nodesNavigator As XPathNavigator = nodes.Current

        Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

        Dim notetext As String = nodesText.Current.Value.ToString() & " name: " & nodesText.Current.Name
        Response.Write(notetext & " ")

Open in new window

0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 150 total points
ID: 40556845
I'm not where I can set up a working example right now,  Just responding to a neglected question alert.

I'm far from a .Net Expert but know more than enough to be dangerous and even less about what I'm about to suggest but I've dabbled and it is really powerful when it comes to .Net and XML.

Use LINQ.

There are a TON of examples out there if you look around:
http://www.codeproject.com/Tips/522456/Reading-XML-using-LINQ

What is the end result of the values once they are extracted?

If you haven't figured it out before I can get back to my development machine (later in the week), I'll see what I can do.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 350 total points
ID: 40559300
You could do it the old fashioned way:
        Dim dom As New XmlDocument
        dom.Load("C:\temp\WX-feed.xml")

        Dim ns As New XmlNamespaceManager(dom.NameTable)
        ns.AddNamespace("x", "http://www.w3.org/2005/Atom")

        Dim nodeList As XmlNodeList = dom.SelectNodes("//x:entry", ns)
        For Each node As XmlNode In nodeList

            Console.WriteLine("Title: " & node("title").InnerText)
            Console.WriteLine("Summary: " & node("summary").InnerText)

        Next

Open in new window

Or using XDocument/Linq:
        Dim doc As XDocument = XDocument.Load("C:\temp\WX-feed.xml")
        Dim ns1 As XNamespace = "http://www.w3.org/2005/Atom"

        Dim entries = _
            From entry In doc.Descendants(ns1 + "entry") _
            Select New With {.Title = entry.Descendants(ns1 + "title").Value, .Summary = entry.Descendants(ns1 + "summary").Value}

        For Each item In entries
            Console.WriteLine("Title: " & item.Title)
            Console.WriteLine("Summary: " & item.Summary)
        Next

Open in new window

Both achieve the same thing.

Note the special handling required to cater for the namespaces in the document.
0
 
LVL 2

Author Closing Comment

by:maredzki
ID: 40567148
Thank you to both of you. I am granting Carl Tawn greater point as he did provide a working example.

Much appreciated!

Marek
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now