public static XElement GetNodeFromPath(XContainer containerNode, ElementPath path) { IEnumerable <XElement> matchingDecendants = containerNode.Descendants(path.PathEnd); XElement node = matchingDecendants.FirstOrDefault(e => GetPathToNode(e).Equals(path)); return(node); }
public static List <string> GetColumnValues(string column, Stream stream) { ElementPath columnPath = new ElementPath(column); XDocument doc = XDocument.Load(stream); string elementName = columnPath.PathEnd; var matchingElements = doc.Descendants(elementName).Where(e => GetPathToNode(e).Equals(columnPath)); List <string> columnValues = matchingElements.Select(e => e.Value).ToList(); return(columnValues); }
public static List <Record> GetRecords(Stream stream, string incidentNode, string responseNode) { XDocument doc = XDocument.Load(stream); ElementPath incidentPath = new ElementPath(incidentNode); ElementPath responsePath = new ElementPath(responseNode); ElementPath incidentParentPath = incidentPath.ParentPath; ElementPath responseParentPath = responsePath.ParentPath; XElement incidentParent = GetNodeFromPath(doc, incidentParentPath); List <Record> records = new List <Record>(); var incidents = incidentParent.Elements(incidentPath.PathEnd).Where(e => GetPathToNode(e).Equals(incidentPath)); foreach (XElement incident in incidents) { // Create a record for each response and add the values XElement responseParent = incident.Descendants(responseParentPath.PathEnd).First(e => GetPathToNode(e).Equals(responseParentPath)); var responses = responseParent.Elements(responsePath.PathEnd).Where(e => GetPathToNode(e).Equals(responsePath)); List <Record> responseRecords = new List <Record>(); foreach (XElement response in responses) { Record record = new Record(); var responseDescendants = response.Descendants(); foreach (XElement descendant in responseDescendants) { ElementPath descendantPath = GetPathToNode(descendant); string descendantValue = descendant.Value; if (!record.Data.ContainsKey(descendantPath.Path)) { record.AddValue(descendantPath.Path, descendantValue); } else { record.AddValue(descendantPath.Path, "MULTIPLE VALUES"); } } responseRecords.Add(record); } // Add the incident values to each response responseParent.Remove(); var incidentDescendants = incident.Descendants(); foreach (XElement descendant in incidentDescendants) { ElementPath descendantPath = GetPathToNode(descendant); string descendantValue = descendant.Value; foreach (Record record in responseRecords) { record.AddValue(descendantPath.Path, descendantValue); } } records.AddRange(responseRecords); } return(records); }