/// <summary> /// Loads the given XML file and tries to parse it into XML. /// </summary> /// <param name="xmlFileName">The location of the XML file to load and parse.</param> void loadAndParseXML(string xmlFileName) { if (File.Exists(xmlFileName)) { TestGeneration = TestGenerations.ValidateXML(xmlFileName); XmlDocument doc = new XmlDocument(); using (FileStream fs = new FileStream(xmlFileName, FileMode.Open, FileAccess.Read)) { doc.Load(fs); FileInfo fi = new FileInfo(xmlFileName); switch (TestGeneration) { case TestGenerations.XML_FIRST_GENERATION: // Dealing with the first typee, test(s) only XmlNodeList testNodes = doc.SelectNodes("//test"); LoadedFileName = fi.Name.Replace(".xml", ""); foreach (XmlNode node in testNodes) { XmlNode sampleFile = node.SelectSingleNode("sample"); XmlNode command = node.SelectSingleNode("cmd"); XmlNode resultFile = node.SelectSingleNode("result"); Entries.Add( new TestEntry( ConvertFolderDelimiters(sampleFile.InnerText), command.InnerText, ConvertFolderDelimiters(resultFile.InnerText) ) ); } break; case TestGenerations.XML_SECOND_GENERATION: // Dealing with multi file foreach (XmlNode node in doc.SelectNodes("//testfile")) { String testFileLocation = ConvertFolderDelimiters(node.SelectSingleNode("location").InnerText); testFileLocation = Path.Combine(fi.DirectoryName, testFileLocation); MultiTest.Add(testFileLocation); } break; case TestGenerations.XML_THIRD_GENERATION: // Dealing with the newest version of tests LoadedFileName = fi.Name.Replace(".xml", ""); foreach (XmlNode node in doc.SelectNodes("//entry")) { int id = int.Parse(node.Attributes["id"].Value); // Get nodes for entry XmlNode command = node.SelectSingleNode("command"); XmlNode input = node.SelectSingleNode("input"); XmlNode output = node.SelectSingleNode("output"); XmlNodeList compareTo = node.SelectSingleNode("compare").SelectNodes("file"); // Convert to appropriate values string ccx_command = command.InnerText; string inputFile = input.InnerText; InputType inputType = InputTypeParser.parseString(input.Attributes["type"].Value); OutputType outputType = OutputTypeParser.parseString(output.InnerText); List <CompareFile> compareFiles = new List <CompareFile>(); foreach (XmlNode compareEntry in compareTo) { bool ignore = bool.Parse(compareEntry.Attributes["ignore"].Value); int compareId = int.Parse(compareEntry.Attributes["id"].Value); string correct = compareEntry.SelectSingleNode("correct").InnerText; XmlNode expectedNode = compareEntry.SelectSingleNode("expected"); string expected = (expectedNode != null) ? expectedNode.InnerText : correct; compareFiles.Add(new CompareFile(compareId, correct, expected, ignore)); } // Add entry Entries.Add(new TestEntry(id, inputFile, inputType, ccx_command, outputType, compareFiles)); } break; default: break; } } return; } throw new InvalidDataException("File does not exist"); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Please specify an input file!"); Console.WriteLine("Usage: convertV1toV3 {input}"); return; } // Check if file exists string firstGen = args[0]; if (!File.Exists(firstGen)) { Console.WriteLine("{0} is not a valid file!", firstGen); return; } // Check if given file is a valid first generation xml try { string generation = TestGenerations.ValidateXML(firstGen); if (generation != TestGenerations.XML_FIRST_GENERATION) { Console.WriteLine("{0} is a valid test file, but not of the first generation!", firstGen); return; } } catch (InvalidDataException) { Console.WriteLine("{0} is not a valid first generation xml test file!", firstGen); return; } // Parse & convert XmlDocument doc = new XmlDocument(); XmlDocument result = new XmlDocument(); result.LoadXml(@"<?xml version=""1.0"" encoding=""UTF - 8""?><tests></tests>"); XmlNode tests = result.SelectSingleNode("tests"); using (FileStream fs = new FileStream(firstGen, FileMode.Open, FileAccess.Read)) { doc.Load(fs); XmlNodeList testNodes = doc.SelectNodes("//test"); foreach (XmlNode node in testNodes) { // Load current data XmlNode sampleFile = node.SelectSingleNode("sample"); XmlNode command = node.SelectSingleNode("cmd"); XmlNode resultFile = node.SelectSingleNode("result"); // Store in new format XmlElement entry = result.CreateElement("entry"); XmlElement entryCommand = result.CreateElement("command"); entryCommand.AppendChild(result.CreateTextNode(command.InnerText)); entry.AppendChild(entryCommand); XmlElement input = result.CreateElement("input"); input.SetAttribute("type", "file"); input.AppendChild(result.CreateTextNode(sampleFile.InnerText)); entry.AppendChild(input); XmlElement output = result.CreateElement("output"); output.AppendChild(result.CreateTextNode("file")); entry.AppendChild(output); XmlElement compare = result.CreateElement("compare"); XmlElement file = result.CreateElement("file"); file.SetAttribute("ignore", "false"); XmlElement correct = result.CreateElement("correct"); correct.AppendChild(result.CreateTextNode(resultFile.InnerText)); file.AppendChild(correct); compare.AppendChild(file); entry.AppendChild(compare); tests.AppendChild(entry); } // Save generated output file using (XmlWriter xw = XmlWriter.Create(firstGen.Substring(0, firstGen.Length - 4) + "_3rd.xml")) { result.WriteTo(xw); } } }