Created
September 12, 2012 14:34
-
-
Save kristopherjohnson/3707029 to your computer and use it in GitHub Desktop.
Extract value of an XML element from a document
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
/// <summary> | |
/// Extract an element value from an XML document | |
/// </summary> | |
/// <param name="xmlDocumentString">XML document string</param> | |
/// <param name="xmlns">XML namespace</param> | |
/// <param name="elementName">name of element whose value is to be returned</param> | |
/// <returns>System.String</returns> | |
/// <exception cref="System.Exception">thrown on failure</exception> | |
public static string GetXmlElementValue(string xmlDocumentString, string xmlns, string elementName) | |
{ | |
var xdoc = XDocument.Parse(xmlDocumentString); | |
XNamespace xns = xmlns; | |
var element = xdoc.Element(xns + elementName); | |
return element.Value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: Say you have this XML:
You can call this:
to get "This is a string value".