How to do it...

  1. Open Visual Studio 2017.
  2. Click File | New | Project to create a project. 
  1. In the New Project dialog box, expand the Other Project Types node in the left-hand pane and select Visual Studio Solutions. In the right-hand pane, select Blank Solution.
  1. In the Name: textbox, type Chapter5.XmlLinq and, in the Location: textbox, select a path from the drop-down box or click on the Browse... button to locate a path:
  1. Click OK.
  2. Now, the Solution Explorer (Ctrl + Alt + L) should look like this:
  1. Now, right-click on the Chapter5.XmlLinq label in the Solution Explorer and select Add | New Project.
  2. In the New Project dialog box, expand the Visual C# node.
  1. Select .NET Standard in the left-hand pane and Class Library (.NET Standard) in the right-hand pane:
  1. Now, in the Name: textbox, type Chapter5.XmlLinq.XmlLinqLib, leave the other defaults as they are, and click OK:
  1. Now, the Solution Explorer (Ctrl + Alt + L) should look like this:
  1. Now, select Class1.cs in the Solution Explorer and press F2 to rename the file XmlBooks.cs.
  2. Answer Yes in the confirmation dialog box that asks to rename the class name as well.
  3. Now double-click on the XmlBooks.cs label in the Solution Explorer.
  4. Add the following using directives next to the last directive in the list:
        using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
  1.  Add the following private variable to store the XML filename:
        private string _xmlFile;
  1. Create the default constructor as follows:
        public XmlBooks(string xmlFile)
{
_xmlFile = xmlFile;
}
  1. Now add the following method to read the XML file:
        public List<string> GetBookTitles()
{

var titles = new List<string>();
XDocument xDoc = XDocument.Load(_xmlFile);

var books = xDoc.Descendants("book");

foreach (var book in books)
{
titles.Add(book.Element("title").Value);
}

return titles;
}
  1. Now, let's press Ctrl + Shift + B for a quick build to check that all the syntax is intact.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.108