[英]How to select multiple nodes based on a maximum child element value in XPath 1.0 (C#, WPF)
[英]How to select nodes with XPath in C#?
简单的问题,我只想从<Template>标签中选择文本。 这就是我所拥有的,但Xpath与任何东西都不匹配。
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);
}
}
您需要使用XmlNamespaceManager
因为Template元素位于命名空间中:
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);
}
这是命名空间问题; 你需要获取名称表,选择一个别名,并在查询中使用它。 或者(在这种情况下)尝试GetElementsByTagName
。
XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace("x",
"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
foreach (XmlNode node in xmlDoc.SelectNodes("//x:Template", mgr))
{
Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}
要么:
foreach (XmlNode node in xmlDoc.GetElementsByTagName("Template"))
{
Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}
这里xpath表达式需要命名空间解析。 您必须实例化XmlNamespaceManager并在SelectNodes中使用它。
这个样本应该有效
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));
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("res", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
foreach (XmlNode node in xmlDoc.SelectNodes("//res:Template", nsmgr))
{
Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}
}
您还可以通过使用和写入来获取默认命名空间
string s = xmlDoc.DocumentElement.GetNamespaceOfPrefix("");
nsmgr.AddNamespace("ns", s);
为什么你还需要命名空间呢? 只是摆脱这些
xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\"
xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\"
你的选择会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.