<?xml version="1.0" ?> <Project ToolsVersion="4.0"> <PropertyGroup Condition="'$(key)'=='1111'"> <Key>Value</Key> </PropertyGroup> </Project>
注意:这不是我使用的实际的XML,它只是更漂亮和更短,并演示了问题.@H_403_4@
IXMLDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition");
它工作正常:@H_403_4@
Condition=”‘$(key)’==’1111′”@H_403_4@
但这不是我的XML@H_403_4@
实际上,我已经包含一个命名空间声明:@H_403_4@
xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″@H_403_4@
制作实际的XML文档:@H_403_4@
<?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");
不返回匹配的节点.@H_403_4@
注意:@H_403_4@
我已经知道了how to query the non-default namespace in xml;你用:@H_403_4@
doc.setProperty("SelectionNamespaces","xmlns="http://schemas.microsoft.com/developer/msbuild/2003");
>我已经知道了how to query the default namespace in .NET.你使用命名空间管理器,给默认命名空间一个名字,然后使用该名称进行查询,那么你可以查询非默认命名空间,因为它不再是默认的
>我可以从我收到的XML字符串中删除令人反感的xmlns文本,但我宁愿“做正确的方法”@H_403_4@
我如何使用MSXML查询“默认”或“未命名”命名空间?@H_403_4@
注意:实际上,我正在使用sql Server的XML ShowPlan输出:@H_403_4@
<?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>
再次,您可以看到有争议的命名空间声明.删除它的工作,但这是乏味的.@H_403_4@
还有什么你试过的?@H_403_4@
我也尝试设置SelectionNamespace:@H_403_4@
doc.setProperty('SelectionNamespaces','xmlns="http://schemas.microsoft.com/developer/msbuild/2003"');
如Microsoft hints at in a KB article.@H_403_4@
我如何获得默认的命名空间?@H_403_4@
实际上我不在乎命名空间.我的查询是有道理的,我希望它工作.那么这个问题的另一种方法可能是:@H_403_4@
How can i query the default namespace whether,or not,and no matter what,that namespace name is (or isn’t)?@H_403_4@
doc.setProperty("SelectionNamespaces","xmlns:peanut='http://schemas.microsoft.com/developer/msbuild/2003'");
IDOMNode node = doc.selectSingleNode("//peanut:PropertyGroup/@Condition");
你可以给这个命名空间你想要的任何缩写名称(在这种情况下是花生).然后使用缩写作为前缀(在这种情况下花生:属性组).@H_403_4@
以前的建议@H_403_4@
我会尝试移动到Xml.Linq.@H_403_4@
这是一个示例(带有命名空间).@H_403_4@
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); }