How to do it...

  1. Open Visual Studio 2017.
  2. Click File | New | Project to create a project. 
  3. 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.
  4. In the Name: textbox, type Chapter5.XmlDoc and, in the Location: textbox, select path from the drop-down box or click on the Browse... button to locate a path:
  1. Click OK.
  1. Now, the Solution Explorer (Ctrl + Alt + L) should look like this:
  1. Now, right-click on the Chapter5.XmlDoc label in the Solution Explorer and select Add | New Project.
  2. In the New Project dialog box, expand the Visual C# node.
  3. 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.XmlDoc.XmlLib, 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 XMLLog.cs.
  2. Answer Yes in the confirmation dialog box that asks to rename the class name as well.
  3. Now double-click on the XMLLog.cs label in the Solution Explorer.
  4. Scroll up until you reach the using directives and add these at the end of the last using directive:
        using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
  1. Now, scroll down and add this class variable on top of the XMLLog class:
        private string _xmlFile;
  1. Let's add the constructor for the XMLLog class:
        public XMLLog(string xmlFile)
{
_xmlFile = xmlFile;
}
  1. Add the following public method after the constructor of the class:
        public void WriteToLog(string message)
{

if (!File.Exists(_xmlFile))
{
using (XmlWriter xmlWriter = XmlWriter.Create(_xmlFile))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Log");
xmlWriter.WriteStartElement("LogEntry");
xmlWriter.WriteElementString("LogDate", DateTime.Now.ToString());
xmlWriter.WriteElementString("Message", message);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();

xmlWriter.Flush();
xmlWriter.Close();
}
}
else
{
XDocument xDoc = XDocument.Load(_xmlFile);
XElement root = xDoc.Element("Log");
IEnumerable<XElement> rows = root.Descendants("LogEntry");

XElement lastRow = rows.Last();
lastRow.AddAfterSelf(
new XElement("LogEntry",
new XElement("LogDate", DateTime.Now.ToString()),
new XElement("Message", message)));

xDoc.Save(_xmlFile);
}
}
  1. Now, add this code next to the WriteToLog() method:
        public Dictionary<string, string> ReadLog()
{

var xmlOutPut = new Dictionary<string, string>();
var line = 0;

if (File.Exists(_xmlFile))
{
using (XmlReader xmlReader = XmlReader.Create(_xmlFile))
{
while(xmlReader.Read())
{

if (xmlReader.IsStartElement())
{

switch (xmlReader.Name)
{
case "LogDate":
xmlOutPut.Add($"LogDate - {line}", xmlReader.ReadElementContentAsString());
break;
case "Message":
xmlOutPut.Add($"Message - {line}", xmlReader.ReadElementContentAsString());
break;
}
}
line++;
}
}

return xmlOutPut;
}
}
  1. Let's press Ctrl + Shift + B for a quick build.
..................Content has been hidden....................

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