Reading XML file with Linq

B

BlackSun

Hi,
I have an Xml file with this structure:

<Root>
<Total name="A">
<Item name="B">
<Donald>AAAA</Donald>
<Donald>BBBB</Donald>
<Donald>CCCC</Donald>
<Donald>DDDD</Donald>
</Item>
<Item name="C">
<Donald>EEEE</Donald>
<Donald>FFFF</Donald>
<Donald>GGGG</Donald>
</Item>

what I need is to get a list of string in this order:
A
B
AAAA
BBBB
CCCC
DDDD
C
EEEE
FFFF
GGGG

How can I get it with linq??

thanks in advance for your help!

Cheers,
BlackSun
 
M

Martin Honnen

BlackSun said:
Hi,
I have an Xml file with this structure:

<Root>
<Total name="A">
<Item name="B">
<Donald>AAAA</Donald>
<Donald>BBBB</Donald>
<Donald>CCCC</Donald>
<Donald>DDDD</Donald>
</Item>
<Item name="C">
<Donald>EEEE</Donald>
<Donald>FFFF</Donald>
<Donald>GGGG</Donald>
</Item>

what I need is to get a list of string in this order:
A
B
AAAA
BBBB
CCCC
DDDD
C
EEEE
FFFF
GGGG

How can I get it with linq??

Here is some solutions using XPath (so it is kind of cheated as you want
a LINQ solution but doing it without XPath makes it hard to get the
order you have above):

'could of course use Dim root As XElement = XElement.Load("file.xml")
'but for the example simply using the XML as an XML literal below

Dim root As XElement = _
<Root>
<Total name="A">
<Item name="B">
<Donald>AAAA</Donald>
<Donald>BBBB</Donald>
<Donald>CCCC</Donald>
<Donald>DDDD</Donald>
</Item>
<Item name="C">
<Donald>EEEE</Donald>
<Donald>FFFF</Donald>
<Donald>GGGG</Donald>
</Item>
</Total>
</Root>

Dim strings As List(Of String) = _
(From nav As XPathNavigator In
root.CreateNavigator().Select("//@name | //text()[normalize-space()]") _
Select nav.Value).ToList()

For Each s In strings
Console.WriteLine(s)
Next

You will need the following Imports:

Imports System
Imports System.Collections.Generic
Imports System.Xml.Linq
Imports System.Xml.XPath
 
B

BlackSun

Il 05/02/2010 13.20, Martin Honnen ha scritto:
BlackSun said:
Hi,
I have an Xml file with this structure:

<Root>
<Total name="A">
<Item name="B">
<Donald>AAAA</Donald>
<Donald>BBBB</Donald>
<Donald>CCCC</Donald>
<Donald>DDDD</Donald>
</Item>
<Item name="C">
<Donald>EEEE</Donald>
<Donald>FFFF</Donald>
<Donald>GGGG</Donald>
</Item>

what I need is to get a list of string in this order:
A
B
AAAA
BBBB
CCCC
DDDD
C
EEEE
FFFF
GGGG

How can I get it with linq??

Here is some solutions using XPath (so it is kind of cheated as you want
a LINQ solution but doing it without XPath makes it hard to get the
order you have above):

'could of course use Dim root As XElement = XElement.Load("file.xml")
'but for the example simply using the XML as an XML literal below

Dim root As XElement = _
<Root>
<Total name="A">
<Item name="B">
<Donald>AAAA</Donald>
<Donald>BBBB</Donald>
<Donald>CCCC</Donald>
<Donald>DDDD</Donald>
</Item>
<Item name="C">
<Donald>EEEE</Donald>
<Donald>FFFF</Donald>
<Donald>GGGG</Donald>
</Item>
</Total>
</Root>

Dim strings As List(Of String) = _
(From nav As XPathNavigator In root.CreateNavigator().Select("//@name |
//text()[normalize-space()]") _
Select nav.Value).ToList()

For Each s In strings
Console.WriteLine(s)
Next

You will need the following Imports:

Imports System
Imports System.Collections.Generic
Imports System.Xml.Linq
Imports System.Xml.XPath
Hi,
thank you for your help!
I get an error on "nav As XPathNavigator" saying "Option Strict On
doesn't allow implic conversion from...."

What I need to do??

Thanks!
 
M

Martin Honnen

BlackSun said:
I get an error on "nav As XPathNavigator" saying "Option Strict On
doesn't allow implic conversion from...."

What I need to do??

Change that statement to

Dim strings As List(Of String) = _
(From nav As XPathNavigator In
root.CreateNavigator().Select("//@name |
//text()[normalize-space()]").Cast(Of XPathNavigator)() _
Select nav.Value).ToList()
 
M

Mr. Arnold

Find out what 'Option Strict On' means and turn it off use Google to
look it up.
 
B

BlackSun

Il 05/02/2010 15.17, Martin Honnen ha scritto:
BlackSun said:
I get an error on "nav As XPathNavigator" saying "Option Strict On
doesn't allow implic conversion from...."

What I need to do??

Change that statement to

Dim strings As List(Of String) = _
(From nav As XPathNavigator In root.CreateNavigator().Select("//@name |
//text()[normalize-space()]").Cast(Of XPathNavigator)() _
Select nav.Value).ToList()

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top