Visual Studio在运行测试时创建“.trx”文件,我正在尝试处理这些文件中的XML。但是,当尝试使用Xpath表达式访问部分XML时,我得到了意想不到的结果。
以下代码包含“.trx”文件的缩减版本。这些文件包含xmlns="..."
属性,似乎可以阻止Xpath访问。当xmlns...
被删除时,访问工作并找到预期的节点。
如何更改名称空间管理器(manager
)或代码中的Xpath(或其他内容),以便我可以获得与未修改的XML中的各种X路径匹配的节点列表。
我尝试在Xpath中添加manager.AddNamespace("ns", trxContent.NamespaceURI);
并包含ns:
,但没有成功。
using System;
using System.Xml;
namespace XpathXmlns
{
class XpathXmlns
{
static string WithXmlns = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<TestRun id=""111"" name=""someName"" runUser=""someUser"" xmlns=""http://microsoft.com/schemas/VisualStudio/TeamTest/2010""> tr1
<TestSettings name=""Local1"" id=""222"" idx=""333""> ts2
<Description>Description 1 context.</Description> ts3
</TestSettings> tr4
<TestSettings name=""Local2"" id=""333"" idx=""444""> ts5
<Description>Description 2 context.</Description> ts6
</TestSettings> tr7
</TestRun>
";
static void Main(string[] args)
{
string WithoutXmlns = WithXmlns.Replace(@" xmlns=""http://microsoft.com/schemas/VisualStudio/TeamTest/2010""", "");
ProcessXml("WithXmnns", WithXmlns);
ProcessXml("WithoutXmlns", WithoutXmlns);
}
static int counter = 0;
static XmlNamespaceManager manager;
static void ProcessXml(string label, string xml)
{
Console.WriteLine(label);
Console.WriteLine(xml);
XmlDocument trxContent = new XmlDocument();
trxContent.LoadXml(xml);
XmlNameTable xmlnt = trxContent.NameTable;
manager = new XmlNamespaceManager(xmlnt);
//manager.AddNamespace("ns", trxContent.NamespaceURI);
XmlNode root = trxContent.DocumentElement;
Extract(trxContent, "//TestRun");
Extract(trxContent, "//TestRun/TestSettings");
Extract(trxContent, "//TestRun/TestSettings/Description");
Extract(trxContent, "//Description");
Extract(trxContent, "//TestSettings/@id");
//Extract(trxContent, "//@id"); // This works OK, it finds the nodes in both cases.
//Extract(trxContent, "//@idx"); // This works OK, it finds the nodes in both cases.
Extract(trxContent, "//ns:TestRun");
Extract(trxContent, "//ns:TestRun/TestSettings");
Extract(trxContent, "//ns:TestRun/TestSettings/Description");
Extract(trxContent, "//ns:Description");
Extract(trxContent, "//ns:TestSettings/@id");
}
static void Extract(XmlDocument doc, string xpath)
{
counter = 0;
ExtractNodes("doc-node", doc, xpath);
ExtractDocNodes("doc", doc, xpath);
Console.WriteLine();
}
private static void ExtractNodes(string source, XmlNode root, string xpath)
{
counter++;
XmlNodeList nodes = root.SelectNodes(xpath, manager);
ListFoundNodes(source, xpath, nodes);
}
private static void ExtractDocNodes(string source, XmlDocument root, string xpath)
{
counter++;
XmlNodeList nodes = root.SelectNodes(xpath, manager);
ListFoundNodes(source, xpath, nodes);
}
private static void ListFoundNodes(string source, string xpath, XmlNodeList nodes)
{
Console.WriteLine(" {0,2}: Get {1} from {2} {3}", counter, nodes.Count, source, xpath);
foreach (XmlNode node in nodes)
{
Console.WriteLine(" Nodes name='{0}'", node.Name ?? "__None__");
}
}
}
}
答案 0 :(得分:3)
Xmlns属性定义元素的xml名称空间。因此,您的TestRun
元素(以及其下的所有元素)属于命名空间http://microsoft.com/schemas/VisualStudio/TeamTest/2010
。首先,您必须将该命名空间添加到管理器:
XmlNameTable xmlnt = trxContent.NameTable;
manager = new XmlNamespaceManager(xmlnt);
manager.AddNamespace("ns", @"http://microsoft.com/schemas/VisualStudio/TeamTest/2010");
如果您不想对其进行硬编码,则可以使用根元素的命名空间:
XmlNode root = trxContent.DocumentElement;
XmlNameTable xmlnt = trxContent.NameTable;
manager = new XmlNamespaceManager(xmlnt);
manager.AddNamespace("ns", root.NamespaceURI);
然后,您必须在查询中使用您定义的名称空间前缀(ns
):
Extract(trxContent, "//ns:TestRun");
// note that all subelements (like TestSettings) are also prefixed
Extract(trxContent, "//ns:TestRun/ns:TestSettings");
Extract(trxContent, "//ns:TestRun/ns:TestSettings/ns:Description");
Extract(trxContent, "//ns:Description");
Extract(trxContent, "//ns:TestSettings/@id");