Using the XElement.Elements method, can I find elements with a wildcard namespace but with the same name?

Trying to perform a simple analysis of an XML document. What is the easiest way to pull two PropertyGroups below?

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     1
  </PropertyGroup>
  <PropertyGroup xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     2
  </PropertyGroup>
</Project>

I am trying to use XElement.Elements (XName), but for this I need a PropertyGroup prefix using xmlns. The problem is that I am not interested in the namespace, and if that changes in the future, I will still like all PropertyGroups.

 var xml = XElement.Load(fileNameWithPath);
 var nameSpace = xml.GetDefaultNamespace();

 var propertyGroups= xml.Elements(nameSpace + "PropertyGroup");

Can you improve this code so that I don’t need to add a name called Space? I know that I can essentially just override the Elements method, but I was hoping there was a way to pass a wildcard namespace?

Thank,

Gavin

+5
source share
1

?

xml.Elements().Where(e => e.Name.LocalName == "PropertyGroup")
+9

All Articles