xml-namespaces – 如何使用MSXML查询默认命名空间
我有一些
XML:
<?xml version="1.0" ?> <Project ToolsVersion="4.0"> <PropertyGroup Condition="'$(key)'=='1111'"> <Key>Value</Key> </PropertyGroup> </Project> 注意:这不是我使用的实际的XML,它只是更漂亮和更短,并演示了问题. 使用MSXML我可以查询节点: IXMLDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition"); 它工作正常:
但这不是我的XML 实际上,我已经包含一个命名空间声明:
制作实际的XML文档: <?xml version="1.0" ?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition="'$(key)'=='1111'"> <Key>Value</Key> </PropertyGroup> </Project> 现在我的查询: IDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition"); 不返回匹配的节点. 如何使用MSXML查询默认名称空间? 注意: 我已经知道了how to query the non-default namespace in xml;你用: doc.setProperty("SelectionNamespaces","xmlns="http://schemas.microsoft.com/developer/msbuild/2003"); >我已经知道了how to query the default namespace in .NET.你使用命名空间管理器,给默认命名空间一个名字,然后使用该名称进行查询,那么你可以查询非默认命名空间,因为它不再是默认的 我如何使用MSXML查询“默认”或“未命名”命名空间? 注意:实际上,我正在使用SQL Server的XML ShowPlan输出: <?xml version="1.0" encoding="UTF-16" standalone="yes"?> <ShowPlanXML Version="1.1" Build="10.50.1600.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"> <BatchSequence> <Batch> ... </Batch> </BatchSequence> </ShowPlanXML> 再次,您可以看到有争议的命名空间声明.删除它的工作,但这是乏味的. 还有什么你试过的? 我也尝试设置SelectionNamespace: doc.setProperty('SelectionNamespaces','xmlns="http://schemas.microsoft.com/developer/msbuild/2003"'); 如Microsoft hints at in a KB article. 我如何获得默认的命名空间? 实际上我不在乎命名空间.我的查询是有道理的,我希望它工作.那么这个问题的另一种方法可能是:
注意:msxml是本机代码,并使用本机的Win32编译器(即没有.NET框架或CLR)
当您将它添加到SelectionNamespaces时,明确地给命名空间一个名称:
doc.setProperty("SelectionNamespaces","xmlns:peanut='http://schemas.microsoft.com/developer/msbuild/2003'"); 然后使用该命名空间进行查询: IDOMNode node = doc.selectSingleNode("//peanut:PropertyGroup/@Condition"); 你可以给这个命名空间你想要的任何缩写名称(在这种情况下是花生).然后使用缩写作为前缀(在这种情况下花生:属性组). 以前的建议 我会尝试移动到Xml.Linq. 这是一个示例(带有命名空间). try { XDocument xDoc1 = XDocument.Parse("<?xml version="1.0" ?><Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup Condition="'$(key)'=='1111'"><Key>Value</Key></PropertyGroup></Project>"); XNamespace ns1 = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); var list1 = from list in xDoc1.Descendants(ns1 + "Project") from item in list.Elements(ns1 + "PropertyGroup") /* where item.Element(ns + "HintPath") != null */ where item.Attribute("Condition") != null select new { MyCondition = item.Attribute("Condition") == null ? "Not Here!" : item.Attribute("Condition").Value,MyFake = item.Attribute("DoesNotExistTest") == null ? "Not Here Sucker!" : item.Attribute("DoesNotExistTest").Value }; foreach (var v in list1) { Console.WriteLine(v.ToString()); } XDocument xDoc2 = XDocument.Parse("<?xml version="1.0" encoding="UTF-16" standalone="yes"?> <ShowPlanXML Version="1.1" Build="10.50.1600.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"> <BatchSequence> <Batch>Something I Threw In Here</Batch> </BatchSequence> </ShowPlanXML> "); XNamespace ns2 = XNamespace.Get("http://schemas.microsoft.com/sqlserver/2004/07/showplan"); var list2 = from list in xDoc2.Descendants(ns2 + "ShowPlanXML") from item in list.Elements(ns2 + "BatchSequence") /* where item.Attribute("Condition") != null */ where item.Element(ns2 + "Batch") != null select new { BatchValue = (item.Element(ns2 + "Batch") == null) ? string.Empty : item.Element(ns2 + "Batch").Value }; foreach (var v in list2) { Console.WriteLine(v.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |