denied
denied

Reputation: 360

Parsing this xml

I tried a lot of codes but nothing worked. I have XML:

<books>
    <book>
        <title>first title</title>
        <publisher>first publisher</publisher>
        <description>first description</description>
        <published>1410</published>
    </book>
    <book>
        <title>second book</title>
        <publisher>second publisher</publisher>
        <description>second description</description>
        <published>1914</published>
    </book>
    [another book]
    [another book2]
</books>

And I want input like this:

first title | first publisher | first description | 1410
second title | second publisher | second descirpion | 1914
[another books]

"My" Code:

var xdoc = XDocument.Load(@"5.xml");
            var entries = from e in xdoc.Descendants("book")
                          select new
                          {
                              Title = (string)e.Element("title"),
                              Description = (string)e.Element("description")
                          };
//I DON'T KNOW WHAT IT DO, I FOUND THIS BUT I DON'T KNOW WHAT NEXT

I can parse first book but i can't parse multiple. Sorry for language.

Upvotes: 1

Views: 84

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

If you want to use an XDocument you may try the following:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        var doc = XDocument.Load("5.xml");
        var books = doc.Descendants("book");
        foreach (var book in books)
        {
            string title = book.Element("title").Value;
            string publisher = book.Element("publisher").Value;
            string description = book.Element("description").Value;
            string published = book.Element("published").Value;
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", title, publisher, description, published);
        }
    }
}

If on the other hand the XML you are trying to parse is very big and cannot fit into memory it is better to use an XmlReader which will allow you to process it record by record:

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        using (var reader = XmlReader.Create("5.xml"))
        {
            string title = null, publisher = null, description = null, published = null;
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "book")
                {
                    Console.WriteLine("{0}\t{1}\t{2}\t{3}", title, publisher, description, published);
                }

                if (reader.NodeType == XmlNodeType.Element && reader.Name == "title")
                {
                    title = reader.ReadInnerXml();
                }

                if (reader.NodeType == XmlNodeType.Element && reader.Name == "publisher")
                {
                    publisher = reader.ReadInnerXml();
                }

                if (reader.NodeType == XmlNodeType.Element && reader.Name == "description")
                {
                    description = reader.ReadInnerXml();
                }

                if (reader.NodeType == XmlNodeType.Element && reader.Name == "published")
                {
                    published = reader.ReadInnerXml();
                }
            }
        }
    }
}

With this approach you can deal with arbitrary large XML files.

Upvotes: 3

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101731

You can use this code to parse your XML

XDocument xDoc = XDocument.Load("5.xml");

var books = (from b in xDoc.Descendants("book")
                select new
                {
                    title = (string) b.Element("title"),
                    publisher = (string) b.Element("publisher"),
                    despription = (string) b.Element("description"),
                    published = (string) b.Element("published")
                }).ToList();

foreach (var book in books)
{
     Console.WriteLine("{0} | {1} | {2}  |{3}",book.title,book.publisher,book.despription,book.published);
}

Upvotes: 1

Related Questions