Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C获取XML文档的选定属性值#_C#_Xml_Visual Studio 2010_C# 4.0 - Fatal编程技术网

C# 使用C获取XML文档的选定属性值#

C# 使用C获取XML文档的选定属性值#,c#,xml,visual-studio-2010,c#-4.0,C#,Xml,Visual Studio 2010,C# 4.0,下面是我的配置xml文件,其中有多个用户名和密码。从中,我需要通过username属性值选择xml节点 <Authentication> <auth Userame="username1" Password ="xxxxxx"/> <auth Userame="username2" Password ="xxxxxxx"/> <auth Userame="username3" Password ="xxxxxx"/>

下面是我的配置xml文件,其中有多个用户名和密码。从中,我需要通过
username
属性值选择xml节点

 <Authentication>
    <auth Userame="username1" Password ="xxxxxx"/>
    <auth Userame="username2" Password ="xxxxxxx"/>
    <auth Userame="username3" Password ="xxxxxx"/>
  </Authentication>

请帮我做这件事。

对代码的XPath部分稍加修改即可:

var username = "username2";
var xpath = String.Format("Settings/Authentication/auth[@Userame='{0}']", username);
XmlDoc.SelectSingleNode(xpath)
      .Attributes["Password"]
      .Value = password;

您是否有身份验证为子级的设置元素?我尝试了您的示例,但是使用了(“身份验证/auth”)而不是(“设置/Authentication/auth”)。而且works@Igor是的,身份验证是设置元素的子元素……那么您的代码有什么问题?@Igor您能告诉我如何使用xmldocument执行此操作吗。@har07,我需要选择具有username2的节点并更新该节点的xml密码值。如何动态分配textbox的值以代替username2。我必须动态分配username2的值并更新密码。尝试如下“XmlDoc.SelectSingleNode(“设置/身份验证/auth[@username=“+username+”])。属性[”密码“].Value;”获取NULLReference Execption.updated我的答案,您可以将
username
变量更改为所需的任何动态值。。
var username = "username2";
var xpath = String.Format("Settings/Authentication/auth[@Userame='{0}']", username);
XmlDoc.SelectSingleNode(xpath)
      .Attributes["Password"]
      .Value = password;