Simple question, I just want to select the text from the <Template> tag. Here's what I have, but the Xpath doesn't match anything.
public static void TestXPath()
{
string xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
xmlText += "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">";
xmlText += "<Template>Normal</Template> <TotalTime>1</TotalTime> <Pages>1</Pages> <Words>6</Words>";
xmlText += "</Properties>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new System.IO.StringReader(xmlText));
foreach (XmlNode node in xmlDoc.SelectNodes("//Template"))
{
Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}
}
A node set is a set of nodes. When you write an XPath expression to return one or more nodes, you call these nodes a node set. For example, if you use the following expression to return a node called title , you will have a set of nodes all called title (assuming there's more than one record).
You need to use an XmlNamespaceManager
because the Template element is in a namespace:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new System.IO.StringReader(xmlText));
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("ns",
"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
foreach (XmlNode node in xmlDoc.SelectNodes("//ns:Template", manager))
{
Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With