-1

I have a xml schema with nested xml elements and following is the small piece of that

<aa>
    <id extension="xx" root="56" />
    <name>demo</name>
    <telecom use="emer" value="tel:34444" />
</aa>

<bb>
   <value value="12345" />
</bb>
<cc>
   <value value="234567" />
</cc>

From this, I have to get the value of "name tag" under "aa tag", last attribute (tel:) of telecom tag, and attribute value of the "value tag" (which is found under bb tag and cc tag)

I tried the following code, but it's not getting exactly what I am expecting.

xDoc.Descendants().Where(x => x.Name.LocalName.Equals("aa")
                              || x.Name.LocalName.Equals("telecom") && 
                              (x.FirstAttribute.Equals("EC")
                               || x.Name.LocalName.Equals("bb")
                               || x.Name.LocalName.Equals("cc"))

Please provide the solution for this issue.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is this your schema? Can it be changed? Having the tel attribute as part of the value attribute difficult. Can you edit with exactly what you hope to get as the result? – Ben Jan 11 '19 at 04:32
  • thanks for replying, it's not my schema, i will fetch this schema while on accessing a service – SiddarthVarunesh Jan 11 '19 at 04:35
  • Can you explain and post an example of exactly what you're trying to select? What do you want to get from your code? – Ben Jan 11 '19 at 04:37
  • I am having a ListCollection to fetch the values from this xml I want to load ("12345" from bb-value tag, "234567" from cc-value tag, name-tag value "demo" , telecom last attribute value (tel:34444) – SiddarthVarunesh Jan 11 '19 at 04:45
  • @SiddarthVarunesh, could you please post exact xml? – er-sho Jan 11 '19 at 05:59

1 Answers1

0

You need to select the appropriate descendant of each of your value and then by using proper linq query you can select desired values

class Program
{
    public static void Main(string[] args)
    {
        XDocument doc = XDocument.Load(@"Path to your xml file");

        var name = doc.Descendants("organisation").Elements().Where(x => x.Name == "name").Select(x => (string)x).FirstOrDefault();
        var tel = doc.Descendants("organisation").Elements().Where(x => x.Name == "telecom").Select(x => x.Attribute("value").Value).FirstOrDefault();
        var bb_value = doc.Descendants("startdate").Elements().Where(x => x.Name == "value").Select(x => x.Attribute("value").Value).FirstOrDefault();
        var cc_value = doc.Descendants("enddate").Elements().Where(x => x.Name == "value").Select(x => x.Attribute("value").Value).FirstOrDefault();

        Console.WriteLine($"name: {name} \ntel: {tel} \nbb_value: {bb_value} \ncc_value: {cc_value}");
        Console.ReadLine();
    }
}

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • Whenever my answer was helpful to you then you can mark the tick on left side of answer to make it green :) – er-sho Jan 11 '19 at 18:01
  • Hi Tried this , but i am returning null value, following is my actual schema will look like (edited due to confidential) demo – SiddarthVarunesh Jan 14 '19 at 03:17
  • your latest provide xml is not in correct format means closing nodes are missing but anyways I updated my answer so just try to replace `aa` with `organisation`, `bb` with `startdate` and `cc` with `enddate` in my answer. see the answer carefully i updated it. and let me know upon same :) – er-sho Jan 14 '19 at 05:40
  • I didn't receive any response from you. does my answer solved your problem? if yes then mark the tick on left side of answer to make it green otherwise I will delete this answer :) – er-sho Feb 02 '19 at 05:29