public Component Find(string FullPath) { // ------------------------------------------------------ // Locates a component with the specified full path // e.g. /RootComponent/Child/SubChild // ------------------------------------------------------ if (FullPath == "") { throw new Exception("Cannot call Find with a blank path"); } if (FullPath[0] != Component.Delimiter) { throw new Exception("Path must be fully qualified: " + FullPath); } int Pos = FullPath.IndexOf(Component.Delimiter, 1); string RootName, NamePath; if (Pos == -1) { RootName = FullPath.Substring(1); NamePath = ""; } else { RootName = FullPath.Substring(1, Pos - 1); NamePath = FullPath.Substring(Pos + 1); } if (RootName.ToLower() != RootComponent.Name.ToLower()) { return(null); } if (NamePath == "") { return(RootComponent); } else { return(RootComponent.Find(NamePath)); } }
public static XmlNode Find(XmlNode Node, string NamePath) { // ---------------------------------------------------- // Find a child with the specified NamePath. NamePath // can be a single child name or a path delimited with // '/' characters e.g. ChildNode/SubChildNode or /RootNode/ChildNode // Returns null if no child found. // ---------------------------------------------------- if (Node == null) { return(null); } if (NamePath == "") { throw new Exception("Cannot call FindByName with a blank path"); } if (NamePath[0] == Delimiter) { Node = Node.OwnerDocument.DocumentElement; int Pos = NamePath.IndexOf(Delimiter, 1); string RootName; if (Pos == -1) { RootName = NamePath.Substring(1); NamePath = ""; } else { RootName = NamePath.Substring(1, Pos - 1); NamePath = NamePath.Substring(Pos + 1); } if (RootName.ToLower() != Name(Node).ToLower()) { return(null); } if (NamePath == "") { return(Node); } } string ChildName, Remainder; int PosDelimiter = NamePath.IndexOf(Delimiter); if (PosDelimiter != -1) { ChildName = NamePath.Substring(0, PosDelimiter); Remainder = NamePath.Substring(PosDelimiter + 1); } else { ChildName = NamePath; Remainder = ""; } if (ChildName == "..") { return(Find(Node.ParentNode, Remainder)); } else if (ChildName.Length > 0 && ChildName[0] == '@') { return(Node.Attributes[ChildName.Substring(1)]); } else { foreach (XmlNode Child in Node.ChildNodes) { if (Name(Child).ToLower() == ChildName.ToLower()) { if (Remainder == "") { return(Child); } else { return(Find(Child, Remainder)); } } } } return(null); }