繁体   English   中英

从后代属性xml linq查找父级?

[英]Find parent from descendant attribute xml linq?

我正在尝试使用子后代属性==值捕获Elements

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load(@"path to doc");
        var query = from q in doc.Elements("Candidates").Elements("Candidates")
                    //How to filter based on descendant attribute value
                    where (string)q.Descendants("CandidatesPropertyValue")
                    .Attributes["PropertyValue"] != "Consumer Sales & Information"
                    //? this doesn't work obviously
                    select q;
        string type;
        string val;
        foreach (var record in query)
        {
            foreach (XAttribute a in record.Element("Candidates").Attributes())
            {
                Console.WriteLine("{0} = \"{1}\"", a.Name.ToString(), a.Value.ToString());
            }
            foreach (XElement e in record.Descendants())
            {
                type = (string)e.Attribute("PropertyType").Value;
                val = (string)e.Attribute("PropertyValue").Value;
                Console.WriteLine("     {0} = \"{1}\"", type, val);
            }
        }
        Console.ReadLine();
    }

<CandidatesRoot>
      I WANT THIS ELEMENT
- <Candidates ...bunch of attributes...>
  <CandidatesPropertyValue PropertyType="Type1" PropertyValue="Value1" /> 
  <CandidatesPropertyValue PropertyType="Type2" PropertyValue="Value2" /> 
  <CandidatesPropertyValue PropertyType="Type3" PropertyValue="Value3" /> 
  <CandidatesPropertyValue PropertyType="Type4" PropertyValue="Value4" /> 
  <CandidatesPropertyValue PropertyType="Type5" PropertyValue="Value5" /> 
  </Candidates>
      BUT I DON'T WANT THIS ONE
- <Candidates ...bunch of attributes...>
  <CandidatesPropertyValue PropertyType="Type1" PropertyValue="Value1" />
  <CandidatesPropertyValue PropertyType="LineOfBusiness" PropertyValue="Consumer Sales & Information" />
  <CandidatesPropertyValue PropertyType="Type2" PropertyValue="Value2" /> 
  <CandidatesPropertyValue PropertyType="Type3" PropertyValue="Value3" /> 
  <CandidatesPropertyValue PropertyType="Type4" PropertyValue="Value4" /> 
  <CandidatesPropertyValue PropertyType="Type5" PropertyValue="Value5" /> 
  </Candidates>

您可以使用XPath

using System.Xml.XPath;

...
String val = "\"Consumer Sales & Information\"";
String xpath = String.Format(".//CandidatesPropertyValue[@PropertyValue= {0}]", val);
doc.XPathSelectElements(xpath);

我要冒险猜测,说您需要将其更改为:

where (string)q.Descendants("CandidatesPropertyValue")
    .Attributes("PropertyValue").SingleOrDefault()

如果它需要至少一个没有该值的后代:

where q.Descendants("CandidatesPropertyValue")
    .Attributes("PropertyValue")
    .Any(a => a.Value != "Consumer Sales & Information")

此举旨在命名的所有元素Candidates谁拥有一个名为至少一个后代CandidatesPropertyValue名为至少一个属性PropertyValue与价值"Consumer Sales & Information"

var query = from q in doc.Elements("Candidates").Elements("Candidates")
            where q.Descendants("CandidatesPropertyValue")
                   .Any(p => p.Attributes("PropertyValue")
                       .Any(a => a.Value == "Consumer Sales & Information"))
            select q;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM