Xml namespaces 如何使用MSXML查询默认命名空间
我有一些XML:Xml namespaces 如何使用MSXML查询默认命名空间,xml-namespaces,msxml,msxml6,Xml Namespaces,Msxml,Msxml6,我有一些XML: <?xml version="1.0" ?> <Project ToolsVersion="4.0"> <PropertyGroup Condition="'$(key)'=='1111'"> <Key>Value</Key> </PropertyGroup> </Project> 而且效果很好: 条件='$key'=='1111' 但这并不是我真正拥有的X
<?xml version="1.0" ?>
<Project ToolsVersion="4.0">
<PropertyGroup Condition="'$(key)'=='1111'">
<Key>Value</Key>
</PropertyGroup>
</Project>
而且效果很好:
条件='$key'=='1111'
但这并不是我真正拥有的XML
实际上,我的XML包含一个名称空间声明:
xmlns=http://schemas.microsoft.com/developer/msbuild/2003
制作实际的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>
不返回匹配的节点
如何使用MSXML查询默认名称空间
注:
我已经知道了;您使用:
doc.setProperty("SelectionNamespaces",
"xmlns="http://schemas.microsoft.com/developer/msbuild/2003");
我已经知道了。使用名称空间管理器,为默认名称空间指定一个名称,然后使用该名称进行查询,然后可以查询非默认名称空间,因为它不再是默认名称空间
我可以从接收到的XML字符串中删除令人反感的xmlns文本,但我更愿意用正确的方法
如何使用MSXML查询默认名称空间或未命名名称空间
注意:实际上,我使用的XML是SQL Server的XML ShowPlan输出:
作为
如何获取默认名称空间?
实际上,我并不关心名称空间。我的问题是有道理的,我希望它能起作用。因此,解决这个问题的另一种方法可能是:
我如何查询默认名称空间,无论名称空间名称是或不是,以及无论是什么
注意:msxml是本机代码,在本机Win32编译器中使用它,即无.NET framework或CLR,当您将名称空间添加到SelectionNamespaces时,显式为其指定名称: 然后使用该名称空间进行查询:
IDOMNode node = doc.selectSingleNode("//peanut:PropertyGroup/@Condition");
在本例中,您可以为该名称空间指定任何您想要的缩写名称。然后在本例中使用缩写作为前缀peant:PropertyGroup
先前的建议
我会尝试转到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);
}
对不起,是IXMLDOMNode,不是IDOMNode。它来自原生msxml COM对象库。这是来自本地的not,即not.NETOk。检查我的更新以获得提示。我猜,但我觉得这比只留下.Net响应要好;成功了。在使用默认名称空间之前,必须命名它,这是非常糟糕的。但至少这是一个可行的解决方案!如果您没有为默认名称空间指定别名,那么它将是不明确的,并且可能会在无名称空间和默认名称空间之间产生冲突。我同意这不是直观的,但也不是没有必要的。不要忘记答案部分的标记,这样其他人就不会不必要地访问这个问题;我投了赞成票,但忘了接受。我在这里多久了?四年/facepalmI又做了一次更新。我从电影院开车回家时被它击中了。@granadaCoder你在看电影时回答了StackOverflow!?在开车回家的路上,我给了这个东西一些CPU周期……我的收音机坏了。
<?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>
doc.setProperty('SelectionNamespaces',
'xmlns="http://schemas.microsoft.com/developer/msbuild/2003"');
doc.setProperty("SelectionNamespaces",
"xmlns:peanut='http://schemas.microsoft.com/developer/msbuild/2003'");
IDOMNode node = doc.selectSingleNode("//peanut:PropertyGroup/@Condition");
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);
}