LINQ to XML handles the specification of namespaces by adding an XNamespace object to an XName object. For example:
PS> [XNameSpace]'http://example/cars' + [XName]'engine' LocalName Namespace NamespaceName --------- --------- ------------- engine http://example/cars http://example/cars
As XNamespace expects to have an XName added to it, casting to that type can be skipped, simplifying the expression:
[XNamespace]'http://example/cars' + 'engine'
A query for an element in a specific namespace will use the following format:
using namespace System.Xml.Linq [XDocument]$xDocument = @" <?xml version="1.0"?> <cars xmlns:c="http://example/cars"> <car type="Saloon"> <c:colour>Green</c:colour> <c:doors>4</c:doors> <c:transmission>Automatic</c:transmission> <c:engine> <size>2.0</size> <cylinders>4</cylinders> </c:engine> </car> </cars> "@ $xNScars = [XNameSpace]'http://example/cars' $xDocument.Descendants('car').ForEach( { $_.Element($xNScars+ 'engine') } )