/// <summary> /// Add paths to the dictionary of paths from an Xml node. /// </summary> /// <param name="node"></param> /// <param name="paths"></param> private static void AddPathsFromXmlNode(XmlNode node, ref Dictionary <string, AttributeSet> paths) { // Don't use our own tags on paths var rel = Strings.ReservedTagsXml.Contains(node.Attributes[Strings.Attributes[XmlAttributeName.Name]].Value) ? "" : node.Attributes[Strings.Attributes[XmlAttributeName.Name]].Value; // Go back up tree until we hit root node to create relative path. var parent = node.ParentNode; while (parent != null && parent.Name?.ToLower() != Strings.ROOT_TAG && parent.Attributes[Strings.Attributes[XmlAttributeName.Name]]?.Value?.ToLower() != Strings.ROOT_TAG) { // Don't use our own tags in paths if (!Strings.ReservedTagsXml.Contains(parent.Attributes[Strings.Attributes[XmlAttributeName.Name]].Value)) { rel = Path.Combine(parent.Attributes[Strings.Attributes[XmlAttributeName.Name]].Value, rel); } parent = parent.ParentNode; } if (node.HasChildNodes) { // Itterate through all sub directories and files. foreach (XmlNode child in node.ChildNodes) { AddPathsFromXmlNode(child, ref paths); } } else if (!paths.Keys.Contains(rel)) { paths.Add(rel, AttributeSet.FromXmlNode(node)); } }
/// <summary> /// Parse Xml file into dictionary of relative paths and attributes. /// <para>Optionally writes structure to project folder (defaults to true).</para> /// </summary> /// <param name="xmlFilePath">Path to the Xml template file.</param> /// <param name="write">If the structure should be written to disk.</param> /// <returns></returns> public static Dictionary <string, AttributeSet> PaseXML(string xmlFilePath, bool write = true) { // Take reference to project and directory to ensure it doesn't change mid method. var project = Strings.CurrentProject; string projectDirectory = Strings.ProjectDirectory; // Read Xml file. var doc = new XmlDocument(); try { doc.Load(xmlFilePath); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "Error"); return(null); } // Skip to root node. var root = doc.ChildNodes[0]; // Create list to hold unique paths and template ids. var paths = new Dictionary <string, AttributeSet>(); // Itterate through XML and populate list. foreach (XmlNode node in root.ChildNodes) { var nodeDir = node.Attributes[Strings.Attributes[XmlAttributeName.Name]].Value; paths.Add(nodeDir, AttributeSet.FromXmlNode(node)); var children = node.ChildNodes; foreach (XmlNode child in children) { AddPathsFromXmlNode(child, ref paths); } } // Write parsed structure to disk if requested. if (write) { WriteStructure(project, projectDirectory, paths); } return(paths); }