对 XMLDocument 的 SelectNodes() 调用不返回任何内容
SelectNodes() call on XMLDocument returning nothing
我有以下简单的 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);
或类似。
我有以下简单的 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);
或类似。