Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Parsing XML with asp.net

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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:
The viewer will learn how to dynamically set the form action using jQuery.

808 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