如何使用MSXML查询默认命名空间

Ian*_*oyd 13 msxml xml-namespaces msxml6

我有一些XML:

<?xml version="1.0" ?>
<Project ToolsVersion="4.0">
    <PropertyGroup Condition="'$(key)'=='1111'">
          <Key>Value</Key>
    </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

注意:这不是我正在使用的实际XML,它只是更漂亮和更短,并演示了这个问题.

使用MSXML我可以查询节点:

IXMLDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition");
Run Code Online (Sandbox Code Playgroud)

它工作正常:

条件= " '$(密钥)' == '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>
Run Code Online (Sandbox Code Playgroud)

现在我的查询:

IDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition");
Run Code Online (Sandbox Code Playgroud)

不返回匹配的节点.

如何使用MSXML查询默认命名空间?

注意:

如何使用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> 
Run Code Online (Sandbox Code Playgroud)

您再次可以看到有问题的命名空间声明.删除它有效,但这很乏味.

你还尝试了什么?

我也试过设置SelectionNamespace:

doc.setProperty('SelectionNamespaces', 
      'xmlns="http://schemas.microsoft.com/developer/msbuild/2003"');
Run Code Online (Sandbox Code Playgroud)

正如微软在知识库文章中暗示的那样.

我如何获得默认命名空间?

实际上我并不关心名称空间.我的查询很有意义,我想让它发挥作用.所以,问题的另一种方法可能是:

我如何查询默认命名空间是否,或者无论如何,该命名空间名称是(或不是)?

注意:msxml是本机代码,并使用本机Win32编译器(即没有.NET框架或CLR)

gra*_*der 14

将名称空间添加到以下内容时,明确地为名称空间指定名称SelectionNamespaces:

doc.setProperty("SelectionNamespaces",
      "xmlns:peanut='http://schemas.microsoft.com/developer/msbuild/2003'");
Run Code Online (Sandbox Code Playgroud)

然后使用该命名空间进行查询:

IDOMNode node = doc.selectSingleNode("//peanut:PropertyGroup/@Condition");
Run Code Online (Sandbox Code Playgroud)

您可以为该命名空间指定所需的任何缩写名称(peanut在本例中).然后使用缩写作为前缀(peanut: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);
    }
Run Code Online (Sandbox Code Playgroud)