Created
June 16, 2013 08:50
-
-
Save harboe/5791438 to your computer and use it in GitHub Desktop.
XElement Extensions: InnerXml, GetAttributeValue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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