How to Read Xaml Content in WPF application and fetch all the UI Control attributes

120 Views Asked by At

I'm trying to read the property of a particular element in XAML file, For that I'm using StreamReader(from System.IO). To get the root element I'm using instance of XmlDocument(from System.Xml). After, getting the root element I used XmlNodeList to get collection of nodes.

Here is the code-snippet:

private string Read(string path)
       {
           var reader = new StreamReader(path);
           var content = new StringBuilder("");
           var line = string.Empty;
           while ((line = reader.ReadLine()) != null) content.Append(line);
           reader.Close();
           return TrimBraces(content.ToString());
       }

private void RecursivelyAddChildNodeName(XmlNodeList childNodeValue)
       {
           foreach (var item in childNodeValue)
           {
               if (((XmlElement) item).Name == "Button")
                   ((XmlElement)item).Attributes["Name"].Value.ToString();
               else if (((XmlElement) item).Name == "Label") AddNameToList(item, "Label");

               if (((XmlNode) item).HasChildNodes) RecursivelyAddChildNodeName(((XmlNode) item).ChildNodes);
           }
       }

Is there a way where all the nodes can be fetched and then traverse through it to find the Objects(like Button, TextBox, CheckBox) and get their attributes(like name, id etc.)?

0

There are 0 best solutions below