Solved

How to join different level of nodes with xml.linq into one dataset row? Need someone to check code.

Posted on 2014-02-27
4
306 Views
Last Modified: 2016-02-10
Hello,
I'm studying Linq to xml, and have below project. Codes is more or less working fine until I choose different level of nodes. Below examples I hope will explain everything.
So, how to join different level of nodes as one table record.

XmlFile:
<books>
    <book ID="0" published="2014">
        <title>Linq2Xml</title>
        <author>
            <first-name>John</first-name>
            <last-name>Brown</last-name>
        </author>
        <publisher>Amazon</publisher>
        <price>50.00</price>
    </book>
</books>

Open in new window




Dim query = From st In statesXml...<author> _
                    Select New With { _
                        .ID = st.@ID,
                        .published = st.@published,
                        .Name = st.<FirstName>.Value _
                                    }
        DataGridView1.DataSource = query.ToList

Open in new window

Return only FirstName Value, like:
ID  published             FirstName
0   | 2014           | <emtpycolumn>






Dim query = From st In statesXml...<book> _
                    Select New With { _
                        .ID = st.@ID,
                        .published = st.@published,
                        .Name = st.<FirstName>.Value _
                                    }
        DataGridView1.DataSource = query.ToList

Open in new window

Return only FirstName Value, like:
ID                               published             FirstName
<emptycolumn> | <emtpycolumn> | John


Thanks in advance for help.
0
Comment
Question by:dejandejanovic
  • 2
  • 2
4 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39892525
Hi dejandejanovic;

The following code snippet should get you what you need. I am not very proficient using the axis operators to get around the document but I can not find axis operater to access the ancestor of a node so the following starts at the ancestor node, book.

Dim query = From st In statesXml...<book> _
             Select New With { _
                .ID = st.@ID, _
                .published = st.@published, _
                .Name = st.<author>.<first-name>.Value _
            }

Open in new window


Please be aware that capitalization in an Linq to XML document is required to access nodes and attributes even though it's not required in Visual Basic to access variables in code.
0
 

Author Closing Comment

by:dejandejanovic
ID: 39892671
Fernando,
works perfect.  Thank you...

I was also try to compose similar as your solution <node>.<childnode>, but somehow I just could not understand, and confgure the syntax.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39892724
You may have had an issue with the spelling of the node name and the capitalization of the node name. For example in your post you used FirstName where in the XML document it was spelled as first-name, and was spelled in lower case characters.
0
 

Author Comment

by:dejandejanovic
ID: 39892738
Yes, I have put in knowledge naming, and casing.
Comparison is wrong as I was very quickly translating from my language to EN, etc... :-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now