繁体   English   中英

c#xml SelectSingleNode问题无法在节点名称中使用变量

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM