Skip to content

Instantly share code, notes, and snippets.

@RichardD2
Created November 8, 2018 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardD2/d03fb96ca4fa50ca95d5d21fe54c5cd2 to your computer and use it in GitHub Desktop.
Save RichardD2/d03fb96ca4fa50ca95d5d21fe54c5cd2 to your computer and use it in GitHub Desktop.
XLinq extension methods to select elements using the default namespace of the container.
using System;
using System.Collections.Generic;
using System.Linq;
namespace System.Xml.Linq
{
public static class XmlDefaultNamespaceExtensions
{
public static IEnumerable<XElement> DescendantsNs(this XElement container, string name)
=> container.Descendants(container.Name.Namespace + name);
public static IEnumerable<XElement> ElementsNs(this XElement container, string name)
=> container.Elements(container.Name.Namespace + name);
public static XElement ElementNs(this XElement container, string name)
=> container.Element(container.Name.Namespace + name);
public static IEnumerable<XElement> DescendantsNs(this IEnumerable<XElement> source, string name)
=> source.SelectMany(el => DescendantsNs(el, name));
public static IEnumerable<XElement> ElementsNs(this IEnumerable<XElement> source, string name)
=> source.SelectMany(el => ElementsNs(el, name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment