Skip to content

Instantly share code, notes, and snippets.

@harboe
Created June 16, 2013 08:50
Show Gist options
  • Save harboe/5791438 to your computer and use it in GitHub Desktop.
Save harboe/5791438 to your computer and use it in GitHub Desktop.
XElement Extensions: InnerXml, GetAttributeValue
using System;
using System.Text;
using System.Xml.Linq;
public static class XElementExtension
{
public static string InnerXml(this XContainer element)
{
var innerXml = new StringBuilder();
// append node's xml string to innerXml
foreach (XNode node in element.Nodes())
innerXml.Append(node.ToString());
return innerXml.ToString();
}
public static XElement InnerElement(this XContainer element)
{
return XElement.Parse(InnerXml(element), LoadOptions.None);
}
public static string GetAttributeValue(this XElement element, string attributeName)
{
// if (string.isNullOrEmpty(attributeName))
// return null;
var attribute = element.Attribute(attributeName);
return attribute == null ? null : attribute.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment