Reading XML file with Linq

  • Thread starter Thread starter BlackSun
  • Start date Start date
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
 
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
 
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!
 
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()
 
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!
 
Back
Top