[英]c# xml SelectSingleNode issue unable to use variable in node name
具有以下代碼行
// Load screen containers
serverTest = document.SelectSingleNode("Server/". this.serverName . "/test").InnerText;
C#不喜歡“。” 級聯字符,不確定在這里做什么。
serverTest是我的課程btw的屬性
Oops使用PHP串聯字符,一個小時前使用該語言。
其中一個模組可以刪除這個模組,抱歉占用空間。
你必須做這樣的事情。
document.SelectSingleNode(@"Server/" + this.serverName + @"/test").InnerText;
對於字符串連接,請使用“ +”加運算符,也可以使用string.Format(如果它包含很多變量)
document.SelectSingleNode(@"Server/" + this.serverName + @"/app").InnerText;
對於大量變量(一旦需要基於屬性的節點檢索,可能會使用多個參數):
// for [Server/localhost/App/MyApp/Module/Admin/Action/Test"
var location = string.Format(@"Server/{0}/App/{1}/Module/{2}/Action/{3}", this.serverName, this.appName, this.moduleName, this.actionName);
document.SelectSingleNode(location).InnerText;
通過將位置與檢索功能分開,您可以輕松調試它並記錄日志,以防任何值不正確。 也使代碼可讀恕我直言。 但是,對於單個值,在大多數情況下可以使用串聯內聯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.