查询xelement中的选择值

时间:2015-08-15 19:34:17

标签: xml linq xelement

我有一个像这样的XMl:

  <?xml version="1.0" encoding="utf-8"?>
  <Words>
     <List>
        <Cid Name="c1" ID="11">
          <Word Name="test1" ID="1">
            <Name>test1</Name>
            <Link>yahoo.com</Link>
          </Word>
        </Cid>
        <Cid Name="c2" ID="22">
          <Word Name="w0" ID="0">
            <Name>test0</Name>
            <Link>yahoo0.com</Link>
          </Word>
          <Word Name="w1" ID="1">
            <Name>test</Name>
            <Link>yahoo.com</Link>
          </Word>
          <Word Name="w1" ID="2">
            <Name>mehrdad</Name>
            <Link>google.com</Link>
          </Word>
        </Cid>
      </List>
  </Words>

我想搜索单词,如果节点存在,则获取名称和链接
所以我尝试使用此代码进行搜索

XDocument doc = XDocument.Load(data);

            var relatedDocs =
                from CatID in
                doc.Elements("Cid")
                     .Where(x => (string)x.Attribute("ID").Value == ctId.ToString())
                     .Select(x => x.Elements("Word"))
                     .Where  (What is here ? don have access to Attribute so what ?)  (x => x.Attribute("Name").Value == sName)

                     select new SelectWord {
                         NameAtt = CatID.Attribute("Name").Value,
                         IDAtt = CatID.Attribute("ID").Value,
                         Name = CatID.Element("Name").Value,
                         Link = CatID.Element("URL").Value
                     };

这是SelectWord类

class SelectWord
        {
            public string NameAtt { get; set; }
            public string IDAtt { get; set; }
            public string Name { get; set; }
            public string Link { get; set; }
        }

,结果总是为空!
我认为我的查询错了,需要改变什么?

0 个答案:

没有答案