MemoryLeak
MemoryLeak

Reputation: 525

Issue fetching xml elements c#

 <?xml version="1.0" encoding="utf-8"?>
 <customUI onLoad="UI_Load" xmlns="somenamespace">
  <commands>
   <command id ="command1" onAction ="ExecuteCommand" />
  </commands>
 <ribbon>
  <tabs>
   <tab id="tab1">
    <group id="group1">
     ....
    </group>
    <group id="group2">
     ....
    </group>
    <group id="group3">
     ....
    </group>
    <group id="group4">
     ....
    </group>
   </tab>
  </tabs>
 </ribbon>
</customUI>

I load the xml from the assembly,

    var assembly = Assembly.GetExecutingAssembly();
    var xml = assembly.GetManifestResourceStream("mynamespace.myxml.xml");

    if (xml != null)
    {
     using (Stream stream = xml)
     {
        XElement xdoc = XElement.Load(stream);
        var elements = xdoc.XPathSelectElements("customUI/ribbon/tabs/tab/group");
     }
   }

xdoc.XPathSelectElements("customUI/ribbon/tabs/tab/group") returns nothing

I then xdoc.Element("customUI") returns null, I can view the xml elements in xdoc when I debug.

What am I doing wrong, why am I not able to view any child elements?

Upvotes: 0

Views: 41

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

You're missing the namespace:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("t", "somenamespace");
var t = xml.XPathSelectElements("/t:customUI/t:ribbon/t:tabs/t:tab/t:group", nsmgr);

You'll need using System.Xml at the top of the file to bring XmlNamespaceManager and NameTable into scope.

Upvotes: 1

Related Questions