All pastes #829103 Raw Edit

Silver

public text v1 · immutable
#829103 ·published 2007-12-22 22:42 UTC
rendered paste body
XPathDocument document = new XPathDocument("source.xml");

// Navigator gets you XPath access to the document.
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager namespaces = new XmlNamespaceManager(navigator.NameTable);

// Can define as many as you like, but you need at least one.
namespaces.AddNamespace("n", "namespace for elements");

// Go go go, XPath!

// Get the contents of the <anotherElement> inside the <rootElement>.
string textContents = (string)xpathNav.Evaluate("string(/n:rootElement/n:anotherElement)", namespaces);

// Get the numeric valid of the 'foo' attribute on the <anotherElement> (XPath 'number' is a double, hence two casts).
int fooValue = (int)(double)subEventNav.Evaluate("number(/n:rootElement/n:anotherElement/@foo)", namespaces);