日期:2014-05-18  浏览次数:20407 次

菜鸟,请教linq问题...
我有个方法,base64解码后得到的字符串是xml格式的内容:如下
C# code

<args token="1">
  <username>hyby_zhangzhen</username>
  <password>*********</password>
  <syncid>hyby_zhangzhen</syncid>
</args>


我想问下,我用linq如何判断<username>是否有值,还要如何把这三条数据的值取出来来呢, 谢谢,用linq

------解决方案--------------------
C# code
    XDocument xmlDoc = XDocument.Parse(@"
<args token=""1"">
  <username>hyby_zhangzhen</username>
  <password>*********</password>
  <syncid>hyby_zhangzhen</syncid>
</args>");
    var q = from c in xmlDoc.Descendants("args")
            where c.Attribute("token").Value == "1"
            select new
            {
              username = c.Element("username"),
              password = c.Element("password"),
              syncid = c.Element("syncid")
            };
    foreach (var item in q)
    {
      Response.Write("<li>" + item.username.Name + ": " + item.username.Value);
      Response.Write("<li>" + item.password.Name + ": " + item.password.Value);
      Response.Write("<li>" + item.syncid.Name + ": " + item.syncid.Value);
    }