我必须在for循环中编写什么代码?

-1 c# linq linq-to-xml windows-phone-7

我需要如下输出,我该怎么做?

       name1
         title1
         title2

       name2
         title1
         title2
Run Code Online (Sandbox Code Playgroud)

在for循环中,我必须显示标题.这里我使用下面的代码,这意味着我收到以下错误:

'char' does not contain a definition for 'Attribute' and no extension method 'Attribute' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference

如何解决上述错误以及如何在for循环中编写条件以显示标题?

我用过这段代码:

   XmlSerializer serializer = new XmlSerializer(typeof(Notchs));
   XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
   var Categories = (from Category in xmlDoc.Descendants("Category")
               select new Notch()
               {
                       name = (string)Category.Attribute("name"),
                       title =  Category.Elements("Articles").Elements("article").ToString()            
               }).ToList();
       foreach(var doc in Categories)
        {
            foreach(var sec in doc.title)
            {
                sec.Attribute("title").Value.ToList(),
            }
        }
        NotchsList.ItemsSource = Categories.ToList();
Run Code Online (Sandbox Code Playgroud)

XML

<Categories>
  <Category name="notchbolly">
   <Articles>
   <article articleid="170" title="Colour And Vibrancy Are Part Of Our DNA">...</      article>
   <article articleid="187" title="German chef gets saucy!!">...</article>
   <article articleid="2004" title="Meet-the-Face-of-Compassion">...</article>
   <article articleid="2005" title="Dutt’s-family-gets-together!">...</article>
   <article articleid="1933" title="NOTCH easy to do recipe- Mushroom Risotto with   truffles">...</article>
   <article articleid="1934" title="NOTCH easy to do recipe- Lobster with Black ink Linguini Arabiatta">...</article>
  <article articleid="1935" title="Yash Birla- 100% Living">...</article>
  </Articles>
</Category>
<Category name="notchfashion">
  <Articles>
  <article articleid="81" title="Making Headlines">...</article>
  <article articleid="99" title="Ladakh through my Lens">...</article>
  <article articleid="689" title="A-Family-Affair">...</article>
  <article articleid="2264" title="Sushmita-back-with-Manav?">...</article>
  <article articleid="70" title="NOTCH- Making of the cover- Farhan Akhtar & Javed Akhtar">...</article>
   <article articleid="78" title="NOTCH easy to do recipe- Aubergine Carpaccio">...       </article>
  <article articleid="1935" title="Yash Birla- 100% Living">...</article>
 </Articles>
</Category>
Run Code Online (Sandbox Code Playgroud)

Ser*_*kiy 5

在您的code title属性中是一个字符串,而不是字符串列表.因此,在内部foreach中,您试图通过char枚举字符串char.你也想从char循环中获取属性.当然,char不是XElement,它没有Attribute方法.

请改用字符串列表(并使用PascalCase名称作为属性).

// why are you named class Notch? use Category!
public class Notch 
{
    public string Name { get; set; }
    public List<string> Titles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这样的事情(没有看到你的xml就无法确定):

XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
var Categories = 
    (from Category in xmlDoc.Descendants("Category")
     select new Notch()
     {
         Name = (string)Category.Attribute("name"),
         Titles = Category.Element("Articles").Elements("article")
                          .Select(a => (string)a.Attribute("title")).ToList()
     }).ToList();

 foreach(var category in Categories)
 {
    foreach(var title in category.Titles)
    {
         // use title
    }
 }

NotchsList.ItemsSource = Categories.ToList();
Run Code Online (Sandbox Code Playgroud)

还有一个建议 - 为短范围变量使用短名称(比如Linq查询中的范围变量).因此,例如上面Category,您可以使用简单而不是变量c.

更新请记住,你的xml中有未转义的&符号.&符号应该逃脱&amp;.

  • @lazyberezovsky +1为你的耐心老兄. (3认同)
  • 他最终会被禁止.可悲的是,他甚至不明白为什么.人们如此粗暴地无视规则,他们的无知坐得如此之深,以至于他们不再意识到这一点. (2认同)