Resolved LINQ TO XML: How to get elements?

loris04

New member
Joined
Oct 27, 2020
Messages
3
Programming Experience
1-3
I have this xml structure:
XMLstructure:
<?xml version="1.0" encoding="utf-8"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
    <asx:values>
        <SALESORDER>
            <EXTENSIONIN>
                <item>
                    <CONFIRMATIONPRINTDATE />
                    <CUSTOMEROFFERNOTE />
                    <CUSTOMERREFERENCE />
                </item>
            </EXTENSIONIN>
            <ORDERCFGSVALUE>
                <item>
                    <CHARC>DRAWING_LANGUAGE_CRM</CHARC>
                </item>
                <item>
                    <CHARC>EOS_QUOT_ORD_TYPE</CHARC>
                </item>
                <item>
                    <CHARC>GRP_BRANCH_EFFICIENCY_FACTOR</CHARC>
                </item>
            </ORDERCFGSVALUE>
        </SALESORDER>
    </asx:values>
</asx:abap>
I need all elements from the elements "item" in the element "ORDERCFGSVALUE". Does anyone know what the correct LINQ query is?
I have tried these query:

Tested queries::
items = from values in xdocument.Descendants("ORDERCFGSVALUE").Elements("item").Elements()
select values;

items = from values in xdocument.Descendants("ORDERCFGSVALUE").Descendants("item").Elements()
select values;

items = from values in xdocument.Descendants("ORDERCFGSVALUE").Descendants("CHARC")
select values;

Unfortunately I get no result.
This query works to get the items of element EXTENSIONIN:


Working query::
items = from values in xdocument.Descendants("EXTENSIONIN").Elements("item").FirstOrDefault().Elements()
select values;

The result should be:
Result:
<CHARC>DRAWING_LANGUAGE_CRM</CHARC>
<CHARC>EOS_QUOT_ORD_TYPE</CHARC>
<CHARC>GRP_BRANCH_EFFICIENCY_FACTOR</CHARC>


Thank you in advance.
 
Last edited by a moderator:
Solution
This worked fine for me:
C#:
            var xdocument = XElement.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?>
<asx:abap xmlns:asx=""http://www.sap.com/abapxml"" version=""1.0"">
    <asx:values>
        <SALESORDER>
            <EXTENSIONIN>
                <item>
                    <CONFIRMATIONPRINTDATE />
                    <CUSTOMEROFFERNOTE />
                    <CUSTOMERREFERENCE />
                </item>
            </EXTENSIONIN>
            <ORDERCFGSVALUE>
                <item>
                    <CHARC>DRAWING_LANGUAGE_CRM</CHARC>
                </item>
                <item>
                    <CHARC>EOS_QUOT_ORD_TYPE</CHARC>
                </item>
                <item>...

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,277
Location
Sydney, Australia
Programming Experience
10+
This worked fine for me:
C#:
            var xdocument = XElement.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?>
<asx:abap xmlns:asx=""http://www.sap.com/abapxml"" version=""1.0"">
    <asx:values>
        <SALESORDER>
            <EXTENSIONIN>
                <item>
                    <CONFIRMATIONPRINTDATE />
                    <CUSTOMEROFFERNOTE />
                    <CUSTOMERREFERENCE />
                </item>
            </EXTENSIONIN>
            <ORDERCFGSVALUE>
                <item>
                    <CHARC>DRAWING_LANGUAGE_CRM</CHARC>
                </item>
                <item>
                    <CHARC>EOS_QUOT_ORD_TYPE</CHARC>
                </item>
                <item>
                    <CHARC>GRP_BRANCH_EFFICIENCY_FACTOR</CHARC>
                </item>
            </ORDERCFGSVALUE>
        </SALESORDER>
    </asx:values>
</asx:abap>");

            var query = xdocument.Descendants("ORDERCFGSVALUE").Descendants("item").Descendants("CHARC");

            var values = query.ToArray();

1603788304145.png
 
Solution

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,277
Location
Sydney, Australia
Programming Experience
10+
Based on the XML you provided, you don't even need filters for the last two calls because there are no other elements:
C#:
var query = xdocument.Descendants("ORDERCFGSVALUE").Descendants().Descendants();
or:
C#:
var query = xdocument.Descendants("ORDERCFGSVALUE").Elements().Elements();
 

loris04

New member
Joined
Oct 27, 2020
Messages
3
Programming Experience
1-3
All three of your "Tested queries" works for me and also get the same result as the query @jmcilhinney posted.

I used XDocument.Load to load the test data file by the way. How have you loaded your xdocument ?
So did I:
C#:
 XDocument xdocument = XDocument.Load(filepath);
But the question has been clarified. Thanks.
 
Top Bottom