using System.Collections.Generic;
using System.Xml.Serialization;
using System.Globalization;
public static void Test()
var departments = dt.AsEnumerable().GroupBy(r => r.Field<string>("Number"));
var root = new XElement("Root",
new XElement("Type", "MyType"),
new XElement("FileDate", "12/15/2022"),
new XElement("Version", "2.0"),
new XElement("Department",
new XElement("Number", d.Key),
new XElement("Id", "0000"),
d.GroupBy(r => (DeptNo : r.Field<string>("DeptNo"), DeptName : r.Field<string>("DeptName"), DeptCode : r.Field<string>("DeptCode"))).Select(
new XElement("DeptNo", deptNo.Key.DeptNo),
new XElement("DeptName", deptNo.Key.DeptName),
new XElement("DeptCode", deptNo.Key.DeptCode),
new XElement("DepartmentData",
new XElement("State", data.Field<string>("State")),
new XElement("Date", data.Field<string>("Date")),
new XElement("Year", data.Field<string>("Year")),
new XElement("ELP", data.Field<string>("ELP"))
var required = XElement.Parse(GetRequiredXml());
required.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Comment).Remove();
Assert.That(XNode.DeepEquals(root, required));
public static DataTable GetDataTable()
Number,DeptNo,DeptName,State,Date,Year,DeptCode,ELP
123A,1001,DESC JR,PA,12/12/2021,2021,74-123,Y
123A,1002,PHIL JR,NY,09/12/2021,2021,74-124,Y
123A,1003,GILB JR,CA,08/12/2021,2021,74-125,N
123A,1004,THEO JR,AZ,07/12/2021,2021,74-126,N
123A,1005,HARR JR,NV,06/12/2021,2021,74-127,Y
123A,1001,DESC JR,FED,06/12/2021,2021,74-123,N
123A,1002,PHIL JR,FED,09/12/2021,2021,74-124,N
Number,DeptNo,DeptName,State,Date,Year,DeptCode,ELP
123A,1001,DESC JR,PA,12/12/2021,2021,74-123,Y
123A,1002,PHIL JR,NY,09/12/2021,2021,74-124,Y
123A,1003,GILB JR,CA,08/12/2021,2021,74-125,N
123A,1001,DESC JR,FED,06/12/2021,2021,74-123,N
123A,1002,PHIL JR,FED,09/12/2021,2021,74-124,N
using (var reader = new StringReader(csvText))
using (var csv = new CsvHelper.CsvReader(reader, CultureInfo.InvariantCulture))
using (var dr = new CsvHelper.CsvDataReader(csv))
var dt = new DataTable();
static string GetRequiredXml() =>
<FileDate>12/15/2022</FileDate>
<DeptName>DESC JR</DeptName>
<DeptCode>74-123</DeptCode>
<Date>06/12/2021</Date> <!-- FIXED -->
<DeptName>PHIL JR</DeptName>
<DeptCode>74-124</DeptCode>
<DeptName>GILB JR</DeptName>
<DeptCode>74-125</DeptCode>
<!-- Other Trust nodes skipped -->
public static void Main()
Console.WriteLine("Environment version: {0} ({1}, {2})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("Failed with unhandled exception: ");
public static class StringHelper
public static int IndexOfFirstDifference(this string a, string b)
int max = Math.Min(a.Length, b.Length);
while (index < max && a[index] == b[index])
if (index >= a.Length || index >= b.Length)