-2

I have the following XML:

<bookstore>
    <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
</bookstore>

I want to read it and display the result as following:

Genre: autobiography
Publication: 1981-03-22
ISBN: 1-861003-11-0
Title: The Autobiography of Benjamin Franklin
Author: Benjamin Franklin
Price: 8.99
Steven
  • 166,672
  • 24
  • 332
  • 435
Jawad Amjad
  • 2,532
  • 2
  • 29
  • 59
  • I suggest you read up on XPath and the `System.Xml` namespace. Also, I'm not sure what Unity has to do with this. – Phylogenesis Dec 23 '14 at 13:50
  • This feels like a 'please give me the codes' kind of question. That's not really what Stackoverflow is meant for. – Steven Dec 24 '14 at 19:35
  • 1
    You might be right but there is nothing bad if someone's doest know the code and asks for it. – Jawad Amjad Dec 24 '14 at 19:41
  • Please don't just ask us to solve the problem for you. Show us how _you_ tried to solve the problem yourself, then show us _exactly_ what the result was, and tell us why you feel it didn't work. See "[What Have You Tried?](http://whathaveyoutried.com/)" for an excellent article that you _really need to read_. – John Saunders Dec 24 '14 at 19:46
  • Jawad, there's a _lot_ wrong when someone doesn't know the code - and doesn't take a little time to search for it. – John Saunders Dec 24 '14 at 19:47
  • Searching for [how to read xml in .net](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=how%20to%20read%20xml%20in%20.net) finds a lot of good hits on the first page, including "[How to Read XML in .NET](http://stackoverflow.com/questions/4752796/how-to-read-xml-in-net), right here in [so]. – John Saunders Dec 24 '14 at 19:49
  • I tried the code and tried to solve it my self. Unfortunately I was not able to solve it. The above xml is also taken from an example which is mentioned on MSDN .net site. You are right I should have posted the tried code. – Jawad Amjad Dec 24 '14 at 19:50
  • Jawad, you're allowed to edit your question to _include_ the code you tried. Please also show the result of the code that you tried. – John Saunders Dec 24 '14 at 20:01
  • Surely I will...after holidays – Jawad Amjad Dec 24 '14 at 20:18

1 Answers1

1

Here you have some example code to do this with XElement:

    var xml = XElement.Load("test.xml");

    foreach (var bookEl in xml.Elements("book"))
    {
        Console.WriteLine("Genre: " + bookEl.Attribute("genre").Value
            + " " + "Publication: " + bookEl.Attribute("publicationdate").Value
            + " " + "ISBN: " + bookEl.Attribute("ISBN").Value);
        Console.WriteLine("Title: " + bookEl.Element("title").Value);
        Console.WriteLine("Author: " + bookEl.Element("author").Element("first-name").Value 
            + " " + bookEl.Element("author").Element("last-name").Value);
        Console.WriteLine("Price: " + bookEl.Element("price").Value);
    }
John
  • 3,627
  • 1
  • 12
  • 13