Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
329 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

722 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