public void NestedNodesAndValues() { Layout layout = new Layout("Test Layout", "/Library", new List <LayoutNode>()); LayoutNode layoutRoot = new LayoutNode("Library Root", "/Library", true, null, new List <LayoutNode>(), new List <LayoutValue>(), new List <LayoutDataTable>()); LayoutNode disksLayoutNode = new LayoutNode("My Disks", "Disks", true, null, new List <LayoutNode>(), new List <LayoutValue>(), new List <LayoutDataTable>()); LayoutNode cdLayoutNode = new LayoutNode("CD", "CD", true, null, new List <LayoutNode>(), new List <LayoutValue>(), new List <LayoutDataTable>()); LayoutValue artistLayoutValue = new LayoutValue("The Artist", "Artist"); layout.Nodes.Add(layoutRoot); layoutRoot.ChildNodes.Add(disksLayoutNode); disksLayoutNode.ChildNodes.Add(cdLayoutNode); cdLayoutNode.Values.Add(artistLayoutValue); ModelParser parser = new ModelParser(layout); XmlModel model = parser.parseXmlString(XML_INPUT); // Validate root level node Assert.AreEqual(1, model.Nodes.Count, "Checking root level node"); XmlModelNode libraryRoot = model.Nodes[0]; Assert.AreEqual("Library Root", libraryRoot.Description); // Second level node Assert.AreEqual(1, libraryRoot.ChildNodes.Count, "Checking second level node"); XmlModelNode disksLevel = libraryRoot.ChildNodes[0]; Assert.AreEqual("My Disks", disksLevel.Description); // Third level node (two instances of CD) Assert.AreEqual(2, disksLevel.ChildNodes.Count); XmlModelNode cd1 = disksLevel.ChildNodes[0]; XmlModelNode cd2 = disksLevel.ChildNodes[1]; Assert.AreEqual("CD", cd1.Description); Assert.AreEqual("CD", cd2.Description); // Values under the third level node Assert.AreEqual(1, cd1.Values.Count); Assert.AreEqual("The Artist", cd1.Values[0].Description); Assert.AreEqual("Michael Jackson", cd1.Values[0].Value); Assert.AreEqual(1, cd2.Values.Count); Assert.AreEqual("The Artist", cd2.Values[0].Description); Assert.AreEqual("Nirvana", cd2.Values[0].Value); }
private List <XmlModelNode> extractXmlModelNodes(LayoutNode layoutNode, XmlNode docOrCurrentNode) { List <XmlModelNode> result = new List <XmlModelNode>(); XmlNodeList foundXmlNodes = docOrCurrentNode.SelectNodes(layoutNode.Xpath); if (foundXmlNodes.Count == 0 && layoutNode.Required) { result.Add(new XmlModelNode(layoutNode.Description, true, null, null, null)); } foreach (XmlNode xmlNode in foundXmlNodes) { List <XmlModelValue> values = extractValues(layoutNode.Values, xmlNode); List <XmlModelDataTable> dataTables = extractDataTables(layoutNode.DataTables, xmlNode); // Call recursively to add the children of this layout node List <XmlModelNode> childNodes = new List <XmlModelNode>(); foreach (LayoutNode layoutChildNode in layoutNode.ChildNodes) { List <XmlModelNode> childrenOfThisNode = extractXmlModelNodes(layoutChildNode, xmlNode); childNodes.AddRange(childrenOfThisNode); } string nodeDescription = layoutNode.Description; if (layoutNode.CustomDescriptionXPath != null) { XmlNode customDescriptionNode = xmlNode.SelectSingleNode(layoutNode.CustomDescriptionXPath); if (customDescriptionNode != null) { nodeDescription = nodeDescription + " [" + (customDescriptionNode.Value != null ? customDescriptionNode.Value : customDescriptionNode.InnerText) + "]"; } else { nodeDescription = nodeDescription + " [" + layoutNode.CustomDescriptionXPath + "]"; } } XmlModelNode newNode = new XmlModelNode(nodeDescription, false, childNodes, values, dataTables); result.Add(newNode); } return(result); }