public GenericXmlReaderResults ProcessFile(string XmlFile, string MappingFile) { // Open the mapping file first GenericXmlMappingSet set = GenericXmlMappingSet.Read(MappingFile); // Return value GenericXmlReaderResults returnValue = new GenericXmlReaderResults(); // Stack to keep all the parent nodes Stack <GenericXmlNode> currentStack = new Stack <GenericXmlNode>(); try { // Get the XML file content, stripped of display tags string content = cleaned_xml_string_from_file(XmlFile, set); // Streams used for reading XmlTextReader readerXml = new XmlTextReader(new StringReader(content)); // Step through the top-level elements while (readerXml.Read()) { if (readerXml.NodeType == XmlNodeType.Element) { // Create the node for this top-level element string nodeName = readerXml.Name; GenericXmlNode topNode = new GenericXmlNode { NodeName = nodeName }; // Add this to the stack currentStack.Push(topNode); // Recursively read this and all children recursive_process_file(readerXml.ReadSubtree(), currentStack, set, returnValue); // Since this node was handled, pop it off the stack currentStack.Pop(); } } } catch (Exception ee) { returnValue.ErrorMessage = ee.Message; } return(returnValue); }
private void recursive_process_file(XmlReader readerXml, Stack <GenericXmlNode> currentStack, GenericXmlMappingSet set, GenericXmlReaderResults returnValue) { // Create the list of nodes to this point from the stack List <GenericXmlNode> currentReverseList = currentStack.ToList(); // THis is now on THIS element, so move to the next, unless there were none if (!readerXml.Read()) { return; } // Create the path for this GenericXmlPath currentPath = new GenericXmlPath(); for (int j = currentReverseList.Count - 1; j >= 0; j--) { currentPath.PathNodes.Add(currentReverseList[j]); } //// First, handle any attributes //if (readerXml.HasAttributes) //{ // // Create the list of nodes to this point // for (int i = 0; i < readerXml.AttributeCount; i++) // { // // Move to this attribute // readerXml.MoveToAttribute(i); // // Create the path for this // GenericXmlPath thisPath = new GenericXmlPath(); // for (int j = currentReverseList.Count - 1; j >= 0; j--) // { // thisPath.PathNodes.Add(currentReverseList[j]); // } // thisPath.AttributeName = readerXml.Name; // // Get the value for this path // string attributeValue = readerXml.Value; // // Does mapping exist for this path? // PathMappingInstructions instructions = set.Get_Matching_Path_Instructions(thisPath); // // If instructions, this was mapped // if ((instructions != null) && (!String.IsNullOrEmpty(instructions.SobekMapping))) // { // MappedValue thisMappedValue = new MappedValue(); // thisMappedValue.Mapping = instructions.SobekMapping; // thisMappedValue.Path = thisPath; // thisMappedValue.Value = attributeValue; // returnValue.MappedValues.Add(thisMappedValue); // } // } // // Move back to the main element after handing the attributes // readerXml.MoveToElement(); //} // Now, iterate through all the child elements that may exist while (readerXml.Read()) { //if (readerXml.NodeType == XmlNodeType.Text) //{ // // Create the path for this // GenericXmlPath thisPath = new GenericXmlPath(); // for (int j = currentReverseList.Count - 1; j >= 0; j--) // { // thisPath.PathNodes.Add(currentReverseList[j]); // } // // Does mapping exist for this path? // PathMappingInstructions instructions = set.Get_Matching_Path_Instructions(thisPath); // // Get the value for this path // string textValue = readerXml.Value; // // If instructions, this was mapped // if ((instructions != null) && ( !String.IsNullOrEmpty(instructions.SobekMapping))) // { // MappedValue thisMappedValue = new MappedValue(); // thisMappedValue.Mapping = instructions.SobekMapping; // thisMappedValue.Path = thisPath; // thisMappedValue.Value = textValue; // returnValue.MappedValues.Add(thisMappedValue); // } //} bool text_found = false; if (readerXml.NodeType == XmlNodeType.Element) { // Create the node for this top-level element string nodeName = readerXml.Name; GenericXmlNode topNode = new GenericXmlNode { NodeName = nodeName }; // Add this to the stack currentStack.Push(topNode); if (topNode.NodeName == "text") { text_found = true; } // It may be that mapping exists right here at this level // Create the path for this currentReverseList = currentStack.ToList(); GenericXmlPath thisPath = new GenericXmlPath(); for (int j = currentReverseList.Count - 1; j >= 0; j--) { thisPath.PathNodes.Add(currentReverseList[j]); } // Does mapping exist for this path? if (set.Contains_Path(thisPath)) { // Collect the attributes FIRST List <Tuple <string, string> > attributes = null; if (readerXml.HasAttributes) { attributes = new List <Tuple <string, string> >(); readerXml.MoveToFirstAttribute(); attributes.Add(new Tuple <string, string>(readerXml.Name, readerXml.Value)); while (readerXml.MoveToNextAttribute()) { attributes.Add(new Tuple <string, string>(readerXml.Name, readerXml.Value)); } readerXml.MoveToElement(); } // Does mapping exist for this path? PathMappingInstructions instructions = set.Get_Matching_Path_Instructions(thisPath); // If instructions, this was mapped if ((instructions != null) && (!String.IsNullOrEmpty(instructions.SobekMapping)) && (instructions.IgnoreSubTree)) { MappedValue thisMappedValue = new MappedValue(); thisMappedValue.Mapping = instructions.SobekMapping; thisMappedValue.Path = thisPath; if (instructions.RetainInnerXmlTags) { thisMappedValue.Value = readerXml.ReadInnerXml(); } else { StringBuilder builder = new StringBuilder(); XmlReader innerReader = readerXml.ReadSubtree(); while (innerReader.Read()) { if (innerReader.NodeType == XmlNodeType.Text) { builder.Append(innerReader.Value); } } thisMappedValue.Value = builder.ToString(); } returnValue.MappedValues.Add(thisMappedValue); //// Actually, there is something here about skipping this //readerXml.Skip(); } else { // Recursively read this and all children recursive_process_file(readerXml.ReadSubtree(), currentStack, set, returnValue); } // Now, handle the attributes if ((attributes != null) && (attributes.Count > 0)) { foreach (Tuple <string, string> attribute in attributes) { // Does mapping exist for this path? PathMappingInstructions attrInstructions = set.Get_Matching_Path_Instructions(thisPath, attribute.Item1); // If instructions, this was mapped if ((attrInstructions != null) && (!String.IsNullOrEmpty(attrInstructions.SobekMapping))) { MappedValue thisMappedValue = new MappedValue(); thisMappedValue.Mapping = attrInstructions.SobekMapping; thisMappedValue.Path = thisPath; thisMappedValue.Path.AttributeName = attribute.Item1; thisMappedValue.Value = attribute.Item2; returnValue.MappedValues.Add(thisMappedValue); } } } } // Since this node was handled, pop it off the stack currentStack.Pop(); } } }