static void Main(string[] args) { var lib = new HelpLibrary(); HelpLibrary.ImportHelpLibraryFromXml("C:\\Users\\Dave\\Desktop\\WPFToolsPanel.xaml", lib); lib.PrintHelpLibrary(); Console.Read(); }
private static void RecursivelyProcess( this XElement element, int depth, Action<XElement, int, HelpLibrary> childAction, Action<XElement, int, HelpLibrary> parentOpenAction, Action<XElement, int> parentCloseAction, HelpLibrary helpLibrary ) { if (element == null) { throw new ArgumentNullException("element"); } if (!element.HasElements) { // Reached the deepest child if (childAction != null) { childAction(element, depth, helpLibrary); } } else { // element has children if (parentOpenAction != null) { parentOpenAction(element, depth, helpLibrary); } depth++; foreach (XElement child in element.Elements()) { child.RecursivelyProcess ( depth, childAction, parentOpenAction, parentCloseAction, helpLibrary ); } depth--; if (parentCloseAction != null) { parentCloseAction(element, depth); } } }
/// <summary> /// Recursively perform operations on a XML element. /// </summary> /// <param name="element"></param> /// <param name="childAction"> /// What to do when an element with no children is reached. /// The XElement is the child element, the int is the depth. /// To do nothing, pass null. /// </param> /// <param name="parentOpenAction"> /// What to do when an element with children is reached. /// The XElement is the parent element, the int is the depth. /// To do nothing, pass null. /// </param> /// <param name="parentCloseAction"> /// What to do when finished processing element with children. /// The XElement is the parent element, the int is the depth. /// To do nothing, pass null. /// </param> public static void RecursivelyProcess( this XElement element, Action<XElement, int, HelpLibrary> childAction, Action<XElement, int, HelpLibrary> parentOpenAction, Action<XElement, int> parentCloseAction, HelpLibrary helpLibrary ) { if (element == null) { throw new ArgumentNullException("element"); } element.RecursivelyProcess ( 0, childAction, parentOpenAction, parentCloseAction, helpLibrary ); }
private static void ProcessChild(XElement child, int depth, HelpLibrary helpLibrary) { List<string> path = new List<string>(); XElement higher = child; do { if (higher.HasAttributes) { path.Add(higher.FirstAttribute.Value); } higher = higher.Parent; } while (higher != null); path.Reverse(); string stringPath = String.Join(" ",path.ToArray()); // Console.WriteLine(stringPath); try { var name = ""; foreach (XAttribute attribute in child.Attributes()) { if (attribute.Name.LocalName == "Name") name = attribute.Value; } if (name == "") { name = child.Name.LocalName; } helpLibrary.Root.Add(name); //helpLibrary.Root.Add(child.Attribute(XName.Get("X:Name")).Value); } catch { Console.WriteLine("Error!"); } // Console.WriteLine("Parent: {0}", child.Parent.FirstAttribute.Value); }
private static void ProcessParentOpen(XElement parent, int depth, HelpLibrary helpLibrary) { List<string> path = new List<string>(); XElement higher = parent; do { if (higher.HasAttributes) { path.Add(higher.FirstAttribute.Value); } higher = higher.Parent; } while (higher != null); path.Reverse(); foreach (string dir in path) { Console.Write(dir + " "); } Console.WriteLine(); }
public static void ImportHelpLibraryFromXml(string filePath, HelpLibrary helpLibrary) { XDocument.Load(filePath).Root.RecursivelyProcess(ProcessChild, null, null, helpLibrary);//ProcessParentClose //ProcessParentOpen }