Aidan Garnish

Collaboration Not Competition

Get XML Node InnerText from SPFile

A method to get the value from an XML node stored in an InfoPath XML document in SharePoint:

public static string GetValueFromSPListItemXml(SPListItem item, string node)
{

SPFile file = item.File;
byte[] xmlFile = file.OpenBinary();
MemoryStream ms = new MemoryStream(xmlFile);
XmlDocument xml = new XmlDocument();
xml.Load(ms);
string s = xml.OuterXml;
XPathDocument x = new XPathDocument(new StringReader(s));
XPathNavigator xPathNav = x.CreateNavigator();
xPathNav.MoveToFollowing(XPathNodeType.Element);
IDictionary<string, string> namespaceDictionary = xPathNav.GetNamespacesInScope(XmlNamespaceScope.All);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("my", namespaceDictionary["my"]);
XmlNode root = xml.DocumentElement;
return root.SelectSingleNode(node, nsmgr).InnerText;

}

 

Example use:

GetValueFromSPListItemXml(item, "//my:Details//my:Title")