0

I have one xml document and I want to read some data from that. The XML document is like below :-

enter image description here

FYI - This is the actual XML file syntax used in our application.

Now I want to read the data from only one group say group name="AutoSaveView". How would I do that ? Is there anyway that I can directly search the section containing group AutoSAve and then I can read the values of different labels from that.

I tried using XDocument the following way:-

    var doc = theFile.ToXML();
    var groups = doc.Element("resources").Element("group");

After that I am unable to get to the AutoSave element.

Bhupesh
  • 35
  • 7
  • var autoSaveGroup = doc.Element("resources").Element("group").Where(x=>x.Attribute("name").Value == "AutoSaveView"); – Grigoris Loukidis May 13 '19 at 15:00
  • You can create a dictionary : Dictionary dict = doc.Descendants("group") .GroupBy(x => (string)x.Attribute("name"), y => y) .ToDictionary(x => x.Key, y => y.First()); – jdweng May 13 '19 at 15:50

1 Answers1

0

I found the solution by implementing the code like below :-

      var xDocument = file.ToXML();
      var xElementResources = xDocument.Element("resources");

      if (xElementResources != null)
      {
        foreach (XElement element in xElementResources.Descendants("group"))
        {
         string groupName = element.Attribute("name")?.Value;
         if (groupName == "AutoSaveView")
         {
          var labelElements = element.Elements("label");

          foreach (var label in labelElements)
          {
             switch (label.FirstAttribute.Value)
             {
                 case "enableAutoSaveCheckEdit1":
                    this.RibbonControlApplicationButtonCaption = label.Value;
                    break;
             }
          }
        }
       }
     }
Bhupesh
  • 35
  • 7