Solved

Parsing XML with asp.net

Posted on 2015-01-09
4
367 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about why website design really matters in today's demanding market.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

16 Experts available now in Live!

Get 1:1 Help Now