From: BlackSun on 4 Feb 2010 18:57 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
From: Martin Honnen on 5 Feb 2010 07:20 BlackSun wrote: > 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 -- Martin Honnen --- MVP XML http://msmvps.com/blogs/martin_honnen/
From: BlackSun on 5 Feb 2010 08:38 Il 05/02/2010 13.20, Martin Honnen ha scritto: > BlackSun wrote: >> 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! -- Ciao BlackSun
From: Martin Honnen on 5 Feb 2010 09:17 BlackSun wrote: > 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() -- Martin Honnen --- MVP XML http://msmvps.com/blogs/martin_honnen/
From: Mr. Arnold on 5 Feb 2010 09:24 Martin Honnen wrote: > BlackSun wrote: > >> I get an error on "nav As XPathNavigator" saying "Option Strict On >> doesn't allow implic conversion from...." >> >> What I need to do?? Find out what 'Option Strict On' means and turn it off use Google to look it up.
|
Next
|
Last
Pages: 1 2 Prev: Error when Running App with Scheduled Task Next: Convert English language text file to Hindi |