对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 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>

和我试图改变其中一个元素的值做以下操作:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(fullPathToPortalPublishSettings);
// Change the publish url to be the one we want
var a = xDoc.SelectNodes("/Project/PropertyGroup");

但它总是爆炸。我已经删除了XML文件顶部的注释,我只尝试了/Project, Project,但我似乎无法理解出了什么问题。我看了其他的帖子,但不知道我的有什么问题。任何想法?谢谢!

您正在查询一个具有名称空间的文档,因此您也需要在代码中处理该名称空间。所以使用命名表,将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);

或类似.

相关内容

  • 没有找到相关文章

最新更新