Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in programming and learning new things following online tutorials and you guys. Nowadays! I am reading and learning about LINQ to XML, I have XML followings that have some element/attribute, how to write a query that retrieves data in the following order.

Order Id =

Order Person details:
Name = Combine firstname and lastname
Phone number details:
xxxxxxxxx (Mobile)
yyyyyyyy (Fax)

Shiping details:
Name:
Address:
City:
Country:

Items details:
Item # 1
Title
Quantity
Price
Note (if it exist)

Item # 2
Title
Quantity
Price
Note (if it exist)

What I have tried:

XML
<shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <orderperson>
        <firstname>Adnan</firstname>
        <lastname>Nazir</lastname>
        <Phones>
            <Phone>
                <Type>Mobile</Type>
                <Number>04567891011</Number>
            </Phone>
            <Phone>
                <Type>Fax</Type>
                <Number>0123456789</Number>
            </Phone>
        </Phones>
    </orderperson>
    <shipto>
        <name>Irfan Nazir</name>
        <address>Langgt 23</address>
        <city>4000 Stavanger</city>
        <country>Germany</country>
    </shipto>
    <items>
        <item>
            <title>Empire Burlesque</title>
            <note>Special Edition</note>
            <quantity>1</quantity>
            <price>10.90</price>
        </item>
        <item>
            <title>Hide your heart</title>
            <quantity>1</quantity>
            <price>9.90</price>
        </item>
    </items>
</shiporder>
Posted
Updated 2-Apr-21 7:30am
v2

1 solution

I think you're better off using serialization. The topic of serialization is honestly too big to cover in a QA answer, but it's easy to find info on the google machine.

There's a lot of prep to use it correctly, but once you've got your entities setup correctly, it comes down to a single line of code to read your xml file.

There are no shortcuts to learning how to do things when you're writing code.

For what it's worth, there are a number of articles posted on CodeProject regarding XML serialization. You should search the site and find an article that explains it in a way you understand.
 
Share this answer
 
v2
Comments
Adi Mirza 2-Apr-21 6:44am    
thank you very much for a tip #realJSOP I will look for it
Garth J Lancaster 3-Apr-21 20:36pm    
as realJSOP said ... look, you COULD write LINQ/XML, but then you're still stuck with getting the three different types of result from the result of that - which could be just as painful .. if you really need the LINQ having looked at deserialisation, you would be better off with three queries, assuming the shipping order has only one orderperson, shipto & then the multiple items can be a collection on their own

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900