Запросите XDocument для элементов по имени на любой глубине

Не имеет ничего общего с ES6, но вам следует упростить ваше состояние с

(A && ¬Y) || (B && ¬X) || (A && B && X && Y)

до

(A || ¬X) && (B || ¬Y)

. Я бы также рекомендовал использовать some вместо map + includes. Оба эти изменения сделают ваш код более удобным для чтения и более эффективным:

filteredClasses() {
  const {gradeFilter, subjectFilter} = this;
  return _.flatMap(this.classes).filter(x =>
    x.name === this.selectedClass.name
    || (!gradeFilter.length || gradeFilter.some(el => el.value === x.grade))
    && (!subjectFilter.length || subjectFilter.some(el => el.value === x.subjectId))
  );
}
140
задан Dave Zych 11 April 2014 в 17:11
поделиться

3 ответа

Потомки должны работать абсолютно прекрасные. Вот пример:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        string xml = @"
<root>
  <child id='1'/>
  <child id='2'>
    <grandchild id='3' />
    <grandchild id='4' />
  </child>
</root>";
        XDocument doc = XDocument.Parse(xml);

        foreach (XElement element in doc.Descendants("grandchild"))
        {
            Console.WriteLine(element);
        }
    }
}

Результаты:

<grandchild id="3" />
<grandchild id="4" />

208
ответ дан 23 November 2019 в 23:13
поделиться

Потомки сделают точно, в чем Вы нуждаетесь, но уверены, что включали имя пространства имен вместе с именем элемента. При исключении его Вы, вероятно, получите пустой список.

22
ответ дан 23 November 2019 в 23:13
поделиться

Пример, указывающий пространство имен:

String TheDocumentContent =
@"
<TheNamespace:root xmlns:TheNamespace = 'http://www.w3.org/2001/XMLSchema' >
   <TheNamespace:GrandParent>
      <TheNamespace:Parent>
         <TheNamespace:Child theName = 'Fred'  />
         <TheNamespace:Child theName = 'Gabi'  />
         <TheNamespace:Child theName = 'George'/>
         <TheNamespace:Child theName = 'Grace' />
         <TheNamespace:Child theName = 'Sam'   />
      </TheNamespace:Parent>
   </TheNamespace:GrandParent>
</TheNamespace:root>
";

XDocument TheDocument = XDocument.Parse( TheDocumentContent );

//Example 1:
var TheElements1 =
from
    AnyElement
in
    TheDocument.Descendants( "{http://www.w3.org/2001/XMLSchema}Child" )
select
    AnyElement;

ResultsTxt.AppendText( TheElements1.Count().ToString() );

//Example 2:
var TheElements2 =
from
    AnyElement
in
    TheDocument.Descendants( "{http://www.w3.org/2001/XMLSchema}Child" )
where
    AnyElement.Attribute( "theName" ).Value.StartsWith( "G" )
select
    AnyElement;

foreach ( XElement CurrentElement in TheElements2 )
{
    ResultsTxt.AppendText( "\r\n" + CurrentElement.Attribute( "theName" ).Value );
}
54
ответ дан 23 November 2019 в 23:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: