Solved

Parsing XML with asp.net

Posted on 2015-01-09
4
381 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
[X]
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
  • 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 77

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ebay messages api 6 18
uploading Theme in Wordpressn - geting error 4 22
Replace value 2 33
Getting robots.txt Error 9 23
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.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

733 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