Skip to content

Instantly share code, notes, and snippets.

@jsheridanwells
Last active March 2, 2018 13:32
Show Gist options
  • Save jsheridanwells/373e4f8738dea5b218bf8c984ad3da9a to your computer and use it in GitHub Desktop.
Save jsheridanwells/373e4f8738dea5b218bf8c984ad3da9a to your computer and use it in GitHub Desktop.
My first basic ML reader/writer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Bootcamp_JeremyWells
{
class Program
{
static void Main(string[] args)
{
// prompt for file input name
Console.WriteLine("Enter the path to the XML file that you want to parse....");
XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");
// prompt for file output
//Console.WriteLine("Enter the name of the file that you want to save to....");
//string outFile = Console.ReadLine();
// write XML to console
XmlNode root = doc.DocumentElement;
// policy effective date
XmlNode effectiveDate = root.SelectSingleNode("Transaction/PAPolicy/EffectiveDate");
Console.WriteLine(effectiveDate.InnerText);
// client email
XmlNode clientEmail = root.SelectSingleNode("Transaction/Client/EmailAddress");
Console.WriteLine(clientEmail.InnerText);
// client phone is first phone number found from HomePhone WorkPhone or MobilePhone
string clientPhone = string.Empty;
string home = root.SelectSingleNode("Transaction/Client/HomePhone").InnerText;
string work = root.SelectSingleNode("Transaction/Client/WorkPhone").InnerText;
string mobile = root.SelectSingleNode("Transaction/Client/MobilePhone").InnerText;
string[] phones = new string[] { home, work, mobile };
foreach (var phone in phones)
{
if (!string.IsNullOrEmpty(phone))
{
clientPhone = phone;
break;
}
}
Console.WriteLine(clientPhone);
// get list of any drivers
XmlNode paPolicy = root.SelectSingleNode("Transaction/PAPolicy");
XmlNodeList driverList = paPolicy.SelectNodes("PADriver");
foreach (XmlNode driver in driverList)
{
// driver first name
Console.WriteLine(driver.SelectSingleNode("DrvFirstName").InnerText);
// driver last name
Console.WriteLine(driver.SelectSingleNode("DrvLastName").InnerText);
}
// count drivers
Console.WriteLine(driverList.Count);
// average driver age
Console.WriteLine(getAverageAge(driverList));
// count vehicles
XmlNodeList vehicleList = paPolicy.SelectNodes("PAVehicle");
Console.WriteLine("number of vehicles: {0}", vehicleList.Count);
// output data to the console (you can use console.out)
// write the data to a new xml file
XmlTextWriter writer = new XmlTextWriter("myXml.xml", null);
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("my title");
writer.WriteElementString("title", book.InnerText);
writer.WriteEndElement();
writer.Close();
private static int getAverageAge(XmlNodeList myList)
{
int age = 0;
foreach (XmlNode driver in myList)
{
DateTime birthday = Convert.ToDateTime(driver.SelectSingleNode("DOB").InnerText);
age += (DateTime.Now.Year - birthday.Year);
}
return age / myList.Count;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment