0

To my great frustration, I spent half a day to build some queries for an XML document and I don't know anything more than I knew before I started. So, I'm taking the easy way out and ask again for help.

The XML code is like this:

<?xml version="1.0" ?>
<Main>
<alpha Id = "AlphaId_1">
    <beta>AlphaId1_Beta</beta>
    <gamma>AlphaId1_Gama</gamma>
    <delta Type = "A">AlphaId1_DeltaTypeA</delta>
    <delta Type = "B">AlphaId1_DeltaTypeB</delta>
    <kapa Id="01">
        <description>AlphaId1_KapaId1_Descr</description>
        <name>AlphaId1KapaId1_Name</name>
        <teta>AlphaId1KapaId1_Teta</teta>
    </kapa>
    <kapa Id="02">
        <description>AlphaId1KapaId2_Descr</description>
        <name>AlphaId1KapaId2_Name</name>
        <teta>AlhaId1KapaId2_Teta</teta>
    </kapa>
</alpha>
<alpha Id = "AlphaId_2">
    <beta>AlphaId2_Beta</beta>
    <gamma>AlphaId2_Gama</gamma>
    <delta Type = "A">AlphaId2_DeltaTypeA</delta>
    <delta Type = "B">AlphaId2_DeltaTypeB</delta>
    <kapa Id="01">
        <description>AlphaId2_KapaId1_Descr</description>
        <name>AlphaId2KapaId2_Name</name>
        <teta>AlphaId2KapaId2_Teta</teta>
    </kapa>
    <kapa Id="02">
        <description>AlphaId1KapaId2_Descr</description>
        <name>AlphaId2KapaId2_Name</name>
        <teta>AlhaId2KapaId2_Teta</teta>
    </kapa>
</alpha>
</Main>

I am looking for a query to retrieve for example the value "AlphaId2_DeltaTypeA". The second query should retrieve all the description values from every KapaId for a selected AlphaId.

The only code I could come up with is

XDocument xdoc = XDocument.Load(@"doc.xml");

IEnumerable<XElement> list1 = xdoc.Root.Descendants("delta");

  var cifmi = 
    from el in list1 
    where   (string)el.Attribute("Type") == "A" 
    select el;

    foreach (XElement el in cifmi)
    {
      textBox1.AppendText(el.Value + System.Environment.NewLine);
    }

The code finds two values instead of one.

0

1 Answer 1

1
var xDoc = XDocument.Load("Input.txt");

var alpha = (from a in xDoc.Root.Elements("alpha")
             let deltas = a.Elements("delta")
             let deltaA = deltas.First(x => (string)x.Attribute("Type") == "A")
             where (string)deltaA == "AlphaId2_DeltaTypeA"
             select a).First();

var descriptions = alpha.Elements("kapa")
                        .Select(x => (string)x.Element("description")).ToList();

It will only look for <delta> which has both Type="A" and value of AlphaId2_DeltaTypeA. If you only care about value try that one:

var alpha = (from a in xDoc.Root.Elements("alpha")
             let deltas = a.Elements("delta")
             where deltas.Any(x => (string)x == "AlphaId2_DeltaTypeA")
             select a).First();

Update

*alpha Id == "AlphaId_1" and delta Type = "A" and the answer is AlphaId1_DeltaTypeA*

var alpha = (string)xDoc.Root
                        .Elements("alpha")
                        .First(x => (string)x.Attribute("Id") == "AlphaId_1")
                        .Elements("delta")
                        .First(x => (string)x.Attribute("Type") == "A");
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Marcin. Can you please make the query so that it looks for the value that meets the conditions alpha Id == "AlphaId_1" and delta Type = "A" and the answer is AlphaId1_DeltaTypeA?
description from my first sample is List<string> so it already contains the values.
Thank you very much Marcin. Sorry for taking so long to validate your answer, I am a bit slow working out the code :) It's all perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.