XML Search

  • Thread starter Thread starter C# newbie
  • Start date Start date
C

C# newbie

Hi Guys,

I know XPath is a good method to search into an xml file but what if
you're looking for a specifc data which could be any where within the xml
file ? Should I consider all possibilities and paths in the xml file ?!!!
Isn't better to have a recurrsive method to traverse the whole tree instead
of passing patterns to "SelectNode" method ?

Let me know if you have experience on this.

Thanks in advace
Newbie
 
XPath is pretty versatile. Any recurrsive method you might write should be
able to be implemented as an XPath expression, but run faster through XPath.
Can you give us an example, maybe someone could give you a snip to try out.
 
Hi Richard,

Thanks for your response but it was kind of general. As far as, I know
it works for XML TagNames but I'm looking for data which are in any place of
an xml file not any tag name. Let's say I have an xml file as below and my
program should find any thing like "#19384A" or "center" in below code. Does
the "GetElementsByTagName" works for this too ?

<?xml version="1.0"?>
<Form SurveyName="empty" SurveyID="1" StyleUsage="URL"
xmlns=http://www.yahoo.com SurveyProgrammer="ramsin" DateTime="2/12/2004
5:00:54 PM">
<Params>
<BackgroundColor>#19384A</BackgroundColor>
<LinkColor>#ffffff</LinkColor>
<VisitedLinkColor>#ffffff</VisitedLinkColor>
<TextColor>#ffffff</TextColor>
<Direction>ltr</Direction>
<BackgroundImage>
</BackgroundImage>
<Header>&lt;table width='600' cellspacing='0' callpadding='0' border='0'
align='center'&gt;
</Params>
....
.....
......
......
......
</Form>

Thanks
 
Let's say I have an xml file as below and my program should find any
thing like "#19384A" or "center" in below code. Does the
"GetElementsByTagName" works for this too ?

You could use an XPath expression like the following to achieve this:

//*[contains(text(), '#19384A') or contains(text(), 'center')]

Just pass that to SelectNodes and you should be set.

Good luck,

-Derek
 
Hi Derek,

Thanks for your response but what you said it's imposibile since
SelectNode() works for nodes not data or string embedded among nodes.



Derek Slager said:
Let's say I have an xml file as below and my program should find any
thing like "#19384A" or "center" in below code. Does the
"GetElementsByTagName" works for this too ?

You could use an XPath expression like the following to achieve this:

//*[contains(text(), '#19384A') or contains(text(), 'center')]

Just pass that to SelectNodes and you should be set.

Good luck,

-Derek
 
Hi again Derek,

I liked your idea but I tried it on below xml code. Please if you get a
chance try it maybe there is something that I'm doing wrong! When I ran

//*[contains(text(), 'b1') or bbb the an excpetion error coems up and
compalins about DataSet!

Any idea ?

- <AAA>

<BBB id="b1" />

<BBB id="b2" />

<BBB name="bbb" />

<BBB />

</AAA>





Derek Slager said:
Let's say I have an xml file as below and my program should find any
thing like "#19384A" or "center" in below code. Does the
"GetElementsByTagName" works for this too ?

You could use an XPath expression like the following to achieve this:

//*[contains(text(), '#19384A') or contains(text(), 'center')]

Just pass that to SelectNodes and you should be set.

Good luck,

-Derek
 
Derek,
I have to appreciate you what you said led me to a good solution. Only some
changes to your query
thanks I appreciate it really.




Derek Slager said:
Let's say I have an xml file as below and my program should find any
thing like "#19384A" or "center" in below code. Does the
"GetElementsByTagName" works for this too ?

You could use an XPath expression like the following to achieve this:

//*[contains(text(), '#19384A') or contains(text(), 'center')]

Just pass that to SelectNodes and you should be set.

Good luck,

-Derek
 
Back
Top