Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parsing XML with asp.net

Posted on 2015-01-09
4
Medium Priority
?
414 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 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 600 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 1400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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:
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…
Suggested Courses

876 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