469,573 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

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

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
Nov 17 '05 #1
15 3059
In message <eb**********************@news.easynews.com>, janhm
<no******@this.adr> writes
Hello.

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


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

--
Steve Walker
Nov 17 '05 #2
thanks, will have a look..
//janhm

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

--
Steve Walker

Nov 17 '05 #3
It appears that XPATH isn't supported, as im developing for a smart device..

any other ideas ?
//Jan

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

--
Steve Walker

Nov 17 '05 #4
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

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

--
Steve Walker


[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #5
In message <mq**********************@news.easynews.com>, janhm
<no******@this.adr> writes
Use an XPATH query. Look up the SelectNodes method of the XmlDataDocument
class.
It appears that XPATH isn't supported, as im developing for a smart device..


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
Nov 17 '05 #6
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.

Nov 17 '05 #7
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

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

Nov 17 '05 #8
In message <eV**************@TK2MSFTNGP12.phx.gbl>, "Richard Blewett
[DevelopMentor]" <ri******@NOSPAMdevelop.com> writes
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.
Of course - that would require COM interop to be supported ;-)


<Homer>Doh!</Homer>

--
Steve Walker
Nov 17 '05 #9
In message <r2**********************@news.easynews.com>, janhm
<no******@this.adr> writes
That's a bit rubbish.
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..


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

--
Steve Walker
Nov 17 '05 #10
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
Nov 17 '05 #11
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
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

Nov 17 '05 #12
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
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


[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #13
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]" <ri******@NOSPAMdevelop.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #14
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'

Nov 17 '05 #15
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]" <ri******@NOSPAMdevelop.com> wrote in
message news:eD*************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Shawn | last post: by
5 posts views Thread by ttomes | last post: by
2 posts views Thread by James Fifth | last post: by
4 posts views Thread by Hooyoo | last post: by
1 post views Thread by Robocop | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.