C# 对XMLDocument调用SelectNodes()时不返回任何内容
我有以下简单的XML文件:C# 对XMLDocument调用SelectNodes()时不返回任何内容,c#,.net,xml,xmldocument,selectnodes,C#,.net,Xml,Xmldocument,Selectnodes,我有以下简单的XML文件: <?xml version="1.0" encoding="utf-8"?> <!-- This file is used by the publish/package process of your Web project. You can customize the behavior of this process by editing this MSBuild file. In order to learn more about thi
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>False</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>E:\PublishTest</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
</PropertyGroup>
</Project>
但它总是爆炸。我已经删除了XML文件顶部的注释,我只是尝试了
/Project
,只是Project
,我似乎无法理解出了什么问题。我看过其他的帖子,但看不出我的有什么问题。有什么想法吗?谢谢 您正在查询一个具有名称空间的文档,因此您还需要在代码中处理该名称空间。因此,请使用nametable,在某个命名空间下注册msbuild命名空间,并按如下方式使用:
XmlNamespaceManager manager = new XmlNamespaceManager(xDoc.NameTable);
manager.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003");
var nodes = xDoc.SelectNodes("//msb:Project/msb:PropertyGroup", manager);
或者类似的。您正在查询一个具有名称空间的文档,因此您还需要在代码中解决这个问题。因此,请使用nametable,在某个命名空间下注册msbuild命名空间,并按如下方式使用:
XmlNamespaceManager manager = new XmlNamespaceManager(xDoc.NameTable);
manager.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003");
var nodes = xDoc.SelectNodes("//msb:Project/msb:PropertyGroup", manager);
或者类似。丢失尾随斜杠,使其成为
/Project/PropertyGroup
或实际选择子项:/Project/PropertyGroup/*
对不起,我应该提到我尝试了“xDoc.SelectNodes(“/Project”)还有,即使这样也不会收回任何东西。你在手表中检查过xDoc吗?它包含你期望的内容吗?是的,它有一个根为#document的文档。不过,我不知道出了什么问题。啊,名称空间…@请参见丢失尾部斜杠,使其成为/Project/PropertyGroup
或实际选择子项:/Project/PropertyGroup/*
对不起,我应该提到我也尝试过“xDoc.SelectNodes(“/Project”),但即使这样也无法收回任何内容。你在手表中检查过xDoc吗?它是否包含您所期望的内容?是的,它的文档根为#document。不过,我不知道出了什么问题。啊,名称空间…@看到了吗