I am using below query to return all the child elements and then based on child node i want to fetch the result.
XElement rootElement = XElement.Load(@"E:\Samples\TestConsole\TestConsoleApp\TestConsoleApp\XMLFile1.xml");
IEnumerable<XElement> lv1s = from lv1 in rootElement.Descendants("A")
where lv1.Attribute("Code").Value.Equals("A001") && lv1.Attribute("Lable").Value.Equals("A001")
select (from ltd in lv1.Descendants("A1")
where ltd.Attribute("Code").Value.Equals("001")
select ltd.Elements()).ToList();
But still i am having following error.
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>>>' to 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>'. An explicit conversion exists (are you missing a cast?)
Please let me know.
i want my value of lv1s[“A11”], lv1s[“A22”]
below is my xml
<?xml version="1.0" encoding="utf-8" ?>
<Document>
<A Code="A001" Lable="A001">
<A1 Code="001">
<A11>A1</A11>
<A22>A2</A22>
<A33>A3</A33>
</A1>
</A>
<A Code="A002" Label="A002">
<A1 Code="002">
<A44>A44</A44>
<A55>A55</A55>
</A1>
</A>
</Document>
Please let me know.
Also How i can handle “Enumeration yielded no results” as count 0 in my return list since it’s giving count>0 if this error occurs.
Result of query has type
IEnumerable<List<IEnumerable<XElement>>>. But you are trying to assign it toIEnumerable<XElement>. Why? Because for eachAelement you are selecting list ofA1child elements. That gives you collection of lists.You can use explicit result type to solve this issue. I.e. instead of
IEnumerable<XElement> lv1susevar lvls. Or use actual lvls type:IEnumerable<List<IEnumerable<XElement>>> lvls.Or if you want to get
IEnumerable<XElement>then useSelectMany:Or rewrite your query this way:
Returns following elements: