I have element path in openXML format like:
/w:document[1]/w:body[1]/w:p[1]
I need to get this element as OpenXmlElement from WordprocessingDocument
Something like this:
public OpenXmlElement GetElementByPath(WordprocessingDocument doc, string path)
{
// Some Logic
return element;
}
Someone, please help
With XPath query (very similar to what you already wrote).
Load the file with XmlDocument and get instance of XPathNavigator from the Document (root) node.
Here is example from my code:
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
public static List<XmlNode> queryXPath(this IXPathNavigable source, String xPath, XmlNamespaceManager nsManager = null)
{
XPathNavigator xNav = source.CreateNavigator();
if (nsManager == null) nsManager = new XmlNamespaceManager(xNav.NameTable);
List<XmlNode> output = new List<XmlNode>();
XPathExpression xExp = XPathExpression.Compile(xPath, nsManager);
XPathNodeIterator xIterator = xNav.Select(xExp);
while (xIterator.MoveNext())
{
XmlNode tmp = xIterator.Current.UnderlyingObject as XmlNode;
output.Add(tmp);
}
return output;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.