2016-10-11 48 views
0

我有以下簡單的XML文件:的SelectNodes()調用返回沒什麼

<?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,我似乎無法理解發生了什麼問題。我看過其他帖子,但沒有看到我的問題。任何想法?謝謝!

+0

丟失斜線使其成爲/ Project/PropertyGroup或實際選擇子項:'/ Project/PropertyGroup/*' – Kris

+0

對不起,我應該提到我試過「xDoc.SelectNodes(」/ Project「)as好吧,甚至沒有任何回退 – skaz

+0

你是否在手錶中檢查過xDoc?它是否包含你期望的內容? – Kris

回答

2

您正在查詢具有命名空間的文檔,因此您需要在代碼中解決該問題。所以使用一個名稱表,註冊一些命名空間下的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); 

或類似的。