public void NodeTypesThatCantHaveChildren () { document.LoadXml ("<foo>bar</foo>"); documentElement = document.DocumentElement; node = documentElement.FirstChild; Assert.AreEqual (node.NodeType, XmlNodeType.Text, "Expected a text node."); Assert.AreEqual (node.HasChildNodes, false, "Shouldn't have children."); Assert.AreEqual (node.ChildNodes.Count, 0, "Should be empty node list."); Assert.AreEqual (node.GetEnumerator().MoveNext(), false, "Should be empty node list."); }
/// <summary> /// Selects child nodes from a parent node satisfying the xpath /// </summary> /// <param name="xmlNode"></param> /// <param name="xpath"></param> /// <returns></returns> public static XmlNodeList SelectNodes (XmlNode xmlNode, string xpath) { XmlNode retXmlNode = xmlNode.Clone(); retXmlNode.RemoveAll(); IEnumerator iEnumerator = xmlNode.GetEnumerator(); while (iEnumerator.MoveNext()) { XmlNode childXmlNode = (XmlNode)iEnumerator.Current; if (childXmlNode.LocalName.ToString() == xpath) { retXmlNode.AppendChild(childXmlNode.CloneNode(true)); } } XmlNodeList retXmlNodeList = retXmlNode.ChildNodes; return retXmlNodeList; }
/// <summary> /// Adds all the images that can be found in the XmlNode /// </summary> /// <param name="node">the XmlNode to search through</param> public void FromXmlNode(XmlNode node) { IEnumerator ienumImages = node.GetEnumerator(); while (ienumImages.MoveNext()) { XmlNode imageNode = (XmlNode)ienumImages.Current; switch (imageNode.Name) { case "fanart": Fanart.Add(new GameImage(imageNode.FirstChild)); break; case "banner": Banners.Add(new GameImage(imageNode)); break; case "screenshot": Screenshots.Add(new GameImage(imageNode.FirstChild)); break; case "boxart": if (imageNode.Attributes.GetNamedItem("side").InnerText == "front") { BoxartFront = new GameImage(imageNode); } else { BoxartBack = new GameImage(imageNode); } break; } } }
/// <summary> /// Adds all the images that can be found in the XmlNode /// </summary> /// <param name="node">the XmlNode to search through</param> public void FromXmlNode(XmlNode node) { IEnumerator ienumImages = node.GetEnumerator(); while (ienumImages.MoveNext()) { XmlNode imageNode = (XmlNode)ienumImages.Current; switch (imageNode.Name) { case "fanart": Fanart.Add(new PlatformImage(imageNode.FirstChild)); break; case "banner": Banners.Add(new PlatformImage(imageNode)); break; case "boxart": Boxart = new PlatformImage(imageNode); break; case "consoleart": ConsoleArt = imageNode.InnerText; break; case "controllerart": ControllerArt = imageNode.InnerText; break; } } }
private void CreateTree(XmlNode node, TreeNode rootNode) { Program.AssertOnEventThread(); if (node.NodeType == XmlNodeType.Text) rootNode.Text = node.Value; else { if (node.Name == "SPECIAL_XS_ENCODED_ELEMENT") rootNode.Text = node.Attributes["name"].Value; else rootNode.Text = node.Name; } IEnumerator ienum = node.GetEnumerator(); while (ienum.MoveNext()) { XmlNode current = (XmlNode)ienum.Current; TreeNode currentNode = new TreeNode(); CreateTree(current, currentNode); rootNode.Nodes.Add(currentNode); } }
/* * Goes through the map and sets the colours up */ private void FindIds(XmlNode root) { IEnumerator ienum = root.GetEnumerator(); while (ienum.MoveNext()) { XmlNode node = (XmlNode)ienum.Current; XmlAttributeCollection attributes = node.Attributes; int index = -1; if(attributes != null) { foreach (XmlAttribute attribute in attributes) { if(attribute.Name == "id") { if(lookUp.ContainsKey(attribute.Value)) { index = lookUp[attribute.Value]; } else if (attribute.Value.Equals("MI-") || attribute.Value.Equals("SP-")) { index = lookUp["MI"]; } } } if(index != -1) { foreach (XmlAttribute attribute in attributes) { if (attribute.Name == "style") { attribute.Value = newColours[result[index]]; } } } } if(node.HasChildNodes) { FindIds(node); } } }
private static bool CompareNodes(XmlNode node1, XmlNode node2) { //compare properties if (node1.Attributes == null || node2.Attributes == null) { bool nullAttributes = (node1.Attributes == null && node2.Attributes == null); if (!nullAttributes) { return false; } } else { if (node1.Attributes.Count != node2.Attributes.Count) { Console.WriteLine("Attributes count: " + node1.Attributes.Count + "!=" + node2.Attributes.Count); Console.WriteLine(node1.ParentNode.InnerXml); Console.WriteLine(node2.ParentNode.InnerXml); return false; } } if (node1.ChildNodes.Count != node2.ChildNodes.Count) { Console.WriteLine("Child nodes count: " + node1.ChildNodes.Count + " !=" + node2.ChildNodes.Count); return false; } if (node1.Name != node2.Name) { Console.WriteLine("Nodes Name: " + node1.Name + "!=" + node2.Name); return false; } if (node1.NodeType != node2.NodeType) { Console.WriteLine("Nodes Type: " + node1.NodeType + "!=" + node2.NodeType); return false; } //the content may have extra spaces or new lines string value1 = ("" + node1.Value).Trim().Replace(Environment.NewLine, ""); string value2 = ("" + node2.Value).Trim().Replace(Environment.NewLine, ""); //replace consecutive whitespaces with one space Regex whiteSpaces = new Regex("\\s+", RegexOptions.Singleline | RegexOptions.Multiline); value1 = whiteSpaces.Replace(value1, " "); value2 = whiteSpaces.Replace(value2, " "); if (value1 != value2) { Console.WriteLine("Nodes value: " + node1.Value + "!=" + node2.Value); return false; } //compare attributes XmlAttribute attribute; if (node1.Attributes != null && node2.Attributes != null) { foreach (XmlAttribute attr in node1.Attributes) { attribute = node2.Attributes[attr.Name]; if (attribute == null) { Console.WriteLine("Null attribute: " + attr.Name); return false; } if (attribute.Value != attr.Value) { Console.WriteLine("Attribute values: " + attribute.Value + "!=" + attr.Value); return false; } } } //compare child nodes IEnumerator enumerator1 = node1.GetEnumerator(); IEnumerator enumerator2 = node2.GetEnumerator(); bool childrenOK = true; while (enumerator1.MoveNext() && enumerator2.MoveNext()) { childrenOK = CompareNodes((XmlNode)enumerator1.Current, (XmlNode)enumerator2.Current); if (!childrenOK) { return false; } } //same properties, same attributes, same child nodes return true; }