Connecting Tech Pros Worldwide Help | Site Map

Need help on looping throug a xml file to find a specific innerxml

janhm
Guest
 
Posts: n/a
#1: Nov 17 '05
Hello.

I need to loop through a xml file finding a specific innerxml text and then
ad the content to a treeview.

For example. If the innerxml im searching for is "item001" then I want the
content in <items></items>

added to my treeview.

<items>
<item>item001</item>
<more_item>stuff</more_item>
</items>

Any ideas how to do that.

I must admit I have little xml and treeview experience so I hope you are
able to help or guide me to an examble.

Thanks

//Janhm


Steve Walker
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


In message <eb6ce.5784503$Zm5.886265@news.easynews.com>, janhm
<no_email@this.adr> writes[color=blue]
>Hello.
>
>I need to loop through a xml file finding a specific innerxml text and then
>ad the content to a treeview.[/color]

Use an XPATH query. Look up the SelectNodes method of the
XmlDataDocument class.

--
Steve Walker
janhm
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


thanks, will have a look..


//janhm

[color=blue]
> Use an XPATH query. Look up the SelectNodes method of the XmlDataDocument
> class.
>
> --
> Steve Walker[/color]


janhm
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


It appears that XPATH isn't supported, as im developing for a smart device..

any other ideas ?


//Jan[color=blue]
>
> Use an XPATH query. Look up the SelectNodes method of the XmlDataDocument
> class.
>
> --
> Steve Walker[/color]


Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


XmlReader r = new XmlTextReader("myfile.xml");

while(r.Read())
{
r.MoveToContent(); // move past whitespace, comments, etc
if( r.NodeType == XmlNodeType.Element && r.LocalName== <nodename> && r.NamespaceUri==<namespace>)
{
r.Read(); // move to the text node of the required element
MessageBox.Show(r.Value); // get the text node value
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

It appears that XPATH isn't supported, as im developing for a smart device..

any other ideas ?


//Jan[color=blue]
>
> Use an XPATH query. Look up the SelectNodes method of the XmlDataDocument
> class.
>
> --
> Steve Walker[/color]



[microsoft.public.dotnet.languages.csharp]
Steve Walker
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


In message <mq9ce.5799851$Zm5.888455@news.easynews.com>, janhm
<no_email@this.adr> writes
[color=blue][color=green]
>> Use an XPATH query. Look up the SelectNodes method of the XmlDataDocument
>> class.[/color][/color]
[color=blue]
>It appears that XPATH isn't supported, as im developing for a smart device..[/color]

That's a bit rubbish.

I was once involved in an eVB project, and I'm pretty sure we used MSXML
to evaluate XPATH expressions on a PDA. I wrote the synchronisation
software in C#, but I think I remember helping the eVB guy with XPATH
syntax.

You might be able to use MSXML via COM interop, but I think it's
probably simpler just to iterate the nodes and examine them.

--
Steve Walker
Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


Of course - that would require COM interop to be supported ;-)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

That's a bit rubbish.

I was once involved in an eVB project, and I'm pretty sure we used MSXML
to evaluate XPATH expressions on a PDA. I wrote the synchronisation
software in C#, but I think I remember helping the eVB guy with XPATH
syntax.

You might be able to use MSXML via COM interop, but I think it's
probably simpler just to iterate the nodes and examine them.

janhm
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


hmm, I don't know so much about this. (comming from delphi programming)..

I got an error and goggled for it.. found this one :

http://groups.google.dk/groups?hl=da...gbl%26rnum%3D1

and a friend told me when developing for smartdevice in vs.net I was using
compact framework. But as I said I don't know..

//Jan

[color=blue]
> That's a bit rubbish.
>
> I was once involved in an eVB project, and I'm pretty sure we used MSXML
> to evaluate XPATH expressions on a PDA. I wrote the synchronisation
> software in C#, but I think I remember helping the eVB guy with XPATH
> syntax.
>
> You might be able to use MSXML via COM interop, but I think it's probably
> simpler just to iterate the nodes and examine them.
>
> --
> Steve Walker[/color]


Steve Walker
Guest
 
Posts: n/a
#9: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


In message <eVGoMECTFHA.3344@TK2MSFTNGP12.phx.gbl>, "Richard Blewett
[DevelopMentor]" <richardb@NOSPAMdevelop.com> writes
[color=blue][color=green]
>>You might be able to use MSXML via COM interop, but I think it's
>>probably simpler just to iterate the nodes and examine them.[/color][/color]
[color=blue]
>Of course - that would require COM interop to be supported ;-)[/color]

<Homer>Doh!</Homer>

--
Steve Walker
Steve Walker
Guest
 
Posts: n/a
#10: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


In message <r2ace.5802539$Zm5.888670@news.easynews.com>, janhm
<no_email@this.adr> writes[color=blue][color=green]
>> That's a bit rubbish.[/color][/color]
[color=blue]
>and a friend told me when developing for smartdevice in vs.net I was using
>compact framework. But as I said I don't know..[/color]

You are, and those XPATH methods aren't supported. I think you'll have
to resort to brute force and ignorance.

--
Steve Walker
Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#11: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


When you say you want a specific innerxml what exactly do you want to do with it?

Do you want to return the xml fragment?
Do you want to process this section internally?
Do you want to return a value from this fragment?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hmm, I don't know so much about this. (comming from delphi programming)..

I got an error and goggled for it.. found this one :

http://groups.google.dk/groups?hl=da...gbl%26rnum%3D1

and a friend told me when developing for smartdevice in vs.net I was using
compact framework. But as I said I don't know..

//Jan


janhm
Guest
 
Posts: n/a
#12: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


When I enter KIOSK001 in my textbox i want to search through the following
xml and look up <kiosknavn>:

<kiosk>
<kiosknavn>KIOSK001</kiosknavn>
<serienummer>099901500001</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
<adresse>adresse</adresse>
<postnummer>postnummer</postnummer>
<by>by</by>
<kontakt_tlf>00000000</kontakt_tlf>
</kiosk>
<kiosk>
<kiosknavn>KIOSK002</kiosknavn>
<serienummer>099901200002</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
.....
</kiosk>

Then I want it to display the entire section between <kiosk>and </kiosk> in
my treeview. but only the section that has the correct <kiosknavn>

hope this makes sense... :)

//Jan
[color=blue]
> When you say you want a specific innerxml what exactly do you want to do
> with it?
>
> Do you want to return the xml fragment?
> Do you want to process this section internally?
> Do you want to return a value from this fragment?
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>[/color]


Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#13: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


How big is the document?

XmlReader is forward only. so you can't get to the kiosknavn element to find the correct one and then back up to the Kiosk element. So the best thing to do is to loop through till you find the kiosk element, extract the subtree then load the subtree into another XmlReader and check if its the correct one, if it is return it otherwise move on to the next one.

while(r.Read())
{
if( r.NodeType == XmlNodeType.Element && r.LocalName == "kiosk")
{
string s = r.GetOuterXml();
// etc.
}
}

Regards

RIchard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

When I enter KIOSK001 in my textbox i want to search through the following
xml and look up <kiosknavn>:

<kiosk>
<kiosknavn>KIOSK001</kiosknavn>
<serienummer>099901500001</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
<adresse>adresse</adresse>
<postnummer>postnummer</postnummer>
<by>by</by>
<kontakt_tlf>00000000</kontakt_tlf>
</kiosk>
<kiosk>
<kiosknavn>KIOSK002</kiosknavn>
<serienummer>099901200002</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
....
</kiosk>

Then I want it to display the entire section between <kiosk>and </kiosk> in
my treeview. but only the section that has the correct <kiosknavn>

hope this makes sense... :)

//Jan
[color=blue]
> When you say you want a specific innerxml what exactly do you want to do
> with it?
>
> Do you want to return the xml fragment?
> Do you want to process this section internally?
> Do you want to return a value from this fragment?
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>[/color]



[microsoft.public.dotnet.languages.csharp]
janhm
Guest
 
Posts: n/a
#14: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


Its not so big yet. is there a size limit ?

I tried the solution below, but got a

'System.Xml.XmlReader' does not contain a definition for 'GetOuterXml'


//Jan

"Richard Blewett [DevelopMentor]" <richardb@NOSPAMdevelop.com> wrote in
message news:%23vh2HcCTFHA.3376@TK2MSFTNGP10.phx.gbl...[color=blue]
> How big is the document?
>
> XmlReader is forward only. so you can't get to the kiosknavn element to
> find the correct one and then back up to the Kiosk element. So the best
> thing to do is to loop through till you find the kiosk element, extract
> the subtree then load the subtree into another XmlReader and check if its
> the correct one, if it is return it otherwise move on to the next one.
>
> while(r.Read())
> {
> if( r.NodeType == XmlNodeType.Element && r.LocalName == "kiosk")
> {
> string s = r.GetOuterXml();
> // etc.
> }
> }
>
> Regards
>
> RIchard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>[/color]


Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#15: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


Sorry that should have been ReadOuterXml() (you could try intellisense ;-) )

if its not big you could load it into an XmlDocument (although I wouldn't normally suggest that on a small device because of the memory overhead and speed). You can traverse backwards in an XmlDocument so you can use the GetElementsByName method as follows:

XmlDocument d = new XmlDocument();
d.Load(<file name>);
XmlNodeList nodes = d.GetElementsByTagName("kiosk");
foreach(XmlNode node in nodes)
{
if( node.FirstChild.FirstChild.Value == <text box value>)
{
return node.OuterXml;
}
}

However, the XmlReader versioj is more efficient

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Its not so big yet. is there a size limit ?

I tried the solution below, but got a

'System.Xml.XmlReader' does not contain a definition for 'GetOuterXml'

janhm
Guest
 
Posts: n/a
#16: Nov 17 '05

re: Need help on looping throug a xml file to find a specific innerxml


ahh yes...ReadOuterXml() worked //been a long day :)

I will look at the other option also,. see what works the best for me...

thanks to both of you.

//Jan

"Richard Blewett [DevelopMentor]" <richardb@NOSPAMdevelop.com> wrote in
message news:eDPYZtCTFHA.208@TK2MSFTNGP10.phx.gbl...[color=blue]
> Sorry that should have been ReadOuterXml() (you could try intellisense
> ;-) )
>
> if its not big you could load it into an XmlDocument (although I wouldn't
> normally suggest that on a small device because of the memory overhead and
> speed). You can traverse backwards in an XmlDocument so you can use the
> GetElementsByName method as follows:
>
> XmlDocument d = new XmlDocument();
> d.Load(<file name>);
> XmlNodeList nodes = d.GetElementsByTagName("kiosk");
> foreach(XmlNode node in nodes)
> {
> if( node.FirstChild.FirstChild.Value == <text box value>)
> {
> return node.OuterXml;
> }
> }
>
> However, the XmlReader versioj is more efficient
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>[/color]


Closed Thread