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
303 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

11 Experts available now in Live!

Get 1:1 Help Now