Read xml

What is a best way of reading xml file? Here is the file I want to ready and my code below. I also want to get  <book id="bk101">.
http://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx

Dim xdoc As New XDocument
 
        Using readerx As New StreamReader(mFile)
            xdoc = XDocument.Load(readerx)
            Dim ArrResults = From c In xdoc.Descendants("book")
            For Each Each_Results As XElement In ArrResults
 
                Dim id = Each_Results.
                Dim author = Each_Results.Element("author").Value
                Dim title = Each_Results.Element("title").Value
 
            Next
        End Using
VBdotnet2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi VBdotnet2005;

I modified the line to retrieve the books id and added a Console.Writeline to display the results.
Dim xdoc As New XDocument
 
Using readerx As New StreamReader(mFile)
    xdoc = XDocument.Load(readerx)
    Dim ArrResults = From c In xdoc.Descendants("book")
    
    For Each Each_Results As XElement In ArrResults
        ' Gets the book id
        Dim id = Each_Results.Attribute("id").Value
        Dim author = Each_Results.Element("author").Value
        Dim title = Each_Results.Element("title").Value
        ' Write out results to the console window
        Console.Writeline("id = {0}  Author = {1}  Title = {2}", id, author, title)
    Next
End Using

' The results of the above code
id = bk101  Author = Gambardella, Matthew  Title = XML Developer's Guide
id = bk102  Author = Ralls, Kim  Title = Midnight Rain
id = bk103  Author = Corets, Eva  Title = Maeve Ascendant
id = bk104  Author = Corets, Eva  Title = Oberon's Legacy
id = bk105  Author = Corets, Eva  Title = The Sundered Grail
id = bk106  Author = Randall, Cynthia  Title = Lover Birds
id = bk107  Author = Thurman, Paula  Title = Splish Splash
id = bk108  Author = Knorr, Stefan  Title = Creepy Crawlies
id = bk109  Author = Kress, Peter  Title = Paradox Lost
id = bk110  Author = O'Brien, Tim  Title = Microsoft .NET: The Programming Bible
id = bk111  Author = O'Brien, Tim  Title = MSXML3: A Comprehensive Guide
id = bk112  Author = Galos, Mike  Title = Visual Studio 7: A Comprehensive Guide

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBdotnet2005Author Commented:
thank you
0
VBdotnet2005Author Commented:
by the way, is this the best way of reading xml?
0
Fernando SotoRetiredCommented:
I like using Linq to XML because it is easier. In answer to your question, in this case yes. It all depends on what you need to query for and do with the results.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.