日期:2014-05-20  浏览次数:20769 次

xpath 查询问题
XML如下:
<?xml   version= "1.0 "   encoding= "utf-8 "   ?>
<Users>
<User>
<UserName> tyq0319 </UserName>
<UserPassword> 219875 </UserPassword>
<UserInfoUrl> UserInfo/tyq0319.xml </UserInfoUrl>
<UserMessage> l; </UserMessage>
<UserPop> ;l </UserPop>
</User>
<User>
<UserName> tyq0319 </UserName>
<UserPassword> 219875 </UserPassword>
<UserInfoUrl> UserInfo/tyq0319.xml </UserInfoUrl>
<UserMessage> l; </UserMessage>
<UserPop> ;l </UserPop>
</User>
</Users>


代码如下:
    protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                XPathNavigator   nav;
                XPathDocument   Xdoc   =   new   XPathDocument( "F:/WebSite2/user/UserLogin.xml ");
                nav=Xdoc.CreateNavigator();
                this.Label1.Text   =   nav.Select( "/Users/User/UserPassword[../UserName= 'tyq0319 '] ").Current.Value;
}


为什么结果是:
        tyq0319219875UserInfo/tyq0319.xmll;;ltyq0319219875UserInfo/tyq0319.xmll;;l   ;

我想根据用户名只得到密码,请问该怎么改?




------解决方案--------------------
XPathNodeIterator iterator = nav.Select( "/Users/User/UserPassword[../UserName= 'tyq0319 '] ");
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current.Value);
}

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------
XmlDocument doc = new XmlDocument();
doc.Load( "your xml file full path ");
XmlNodeList users = doc.SelectNodes( "Users/User ");
foreach(XmlNode user in users) {
if(user.ChildNodes[ "UserName "].InnerText == "tyq0319 ") {
string userPassword = user.ChildNodes[ "UserPassword "].InnerText;
}
}

// "[] "这里面好像只能写XmlAttribute的值吧,如 <Node att1 = "a value "/> ,XPath = "Node[@att1 = 'a value '] "
------解决方案--------------------
像你这像的情况,可以先找出UserName,然后再找它的得到它的ParentNode,再找到它的UserPassword

------解决方案--------------------
JIEFEN 接分啊