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
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 63

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 63

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

623 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