How to query default namespace with MSXML

前端 未结 1 1174
刺人心
刺人心 2021-02-07 15:54

I have some XML:



    
          

        
相关标签:
1条回答
  • 2021-02-07 16:05

    Explicitly give the namespace a name when you add it to the SelectionNamespaces:

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

    and then query using that namespace:

    IDOMNode node = doc.selectSingleNode("//peanut:PropertyGroup/@Condition");
    

    You can give that namespace any abbreviation name you want (peanut in this case). And then use the abbreviation as prefix (peanut:PropertyGroup in this case).

    Earlier suggestions

    I would try moving to Xml.Linq.

    Here is a sample (with a namespace).

          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);
        }
    
    0 讨论(0)
提交回复
热议问题