469,291 Members | 1,692 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

XmlDocument.SelectNodes()

Hi
I'am using SelectNodes method to find if node followed by string exists, and
if so i going to select that node.

Create doc i such a way:
doc = new XmlDocument();

doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));

docHandle = doc.CreateElement("Base");

doc.AppendChild(docHandle);

After that :

nodes = doc.SelectNodes(path);

IMy xmldocument represents directory structure, and before i add element i
check if each of directories ( dir1\dir2\...dirn ) exists.

Unfortunately 'nodes = doc.SelectNodes(path)' raises an exception 'Invalid
token in path'.

For any help - thanks


--

Pozdrawiam
Piotr Kolodziej
http://piotrkolodziej.waikru.info
Feb 19 '06 #1
16 19732
Hello PiotrKolodziej,

Does the Path query finds the node in your XML?
Can u test the query here http://www.activsoftware.com/xml/xpath/ first to
check it u are requesting the right nodes?

P> Hi
P> I'am using SelectNodes method to find if node followed by string
P> exists, and
P> if so i going to select that node.
P> Create doc i such a way:
P> doc = new XmlDocument();
P> doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
P> docHandle = doc.CreateElement("Base");
P> doc.AppendChild(docHandle);
P> After that :
P> nodes = doc.SelectNodes(path);
P> IMy xmldocument represents directory structure, and before i add
P> element i check if each of directories ( dir1\dir2\...dirn ) exists.
P>
P> Unfortunately 'nodes = doc.SelectNodes(path)' raises an exception
P> 'Invalid token in path'.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Feb 19 '06 #2
> Does the Path query finds the node in your XML?
Can u test the query here http://www.activsoftware.com/xml/xpath/ first to
check it u are requesting the right nodes?


The node is not always present in XMLdoc. In fact i'am using it in such a
way:
IF the node exists, select the nodes.
Important is that i want to find the node starting from the parent node.
Feb 19 '06 #3
PiotrKolodziej <pi*************@gmail.com> wrote:
I'am using SelectNodes method to find if node followed by string exists, and
if so i going to select that node.
<snip>
Unfortunately 'nodes = doc.SelectNodes(path)' raises an exception 'Invalid
token in path'.


Well, that sounds like it's a problem with the path you're using -
which unfortunately you didn't post.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 19 '06 #4
> Well, that sounds like it's a problem with the path you're using -
which unfortunately you didn't post.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.


Unfortunatly method is 1k lines and it's hard to get what is important.
Well. I also tought that path might me wrong. It's not.
I checked it for invalid characters and so on.
Now i'am beginning to think that maybe i shouln't use SelectNodes method to
select nodes that doesn't exists.
PK
Feb 19 '06 #5
Anyway here is first ver. of the method:

static private XmlNode checkEgsistance(ref ArrayList mList)

{

string path = null;

XmlNodeList nodes = null, temp = null;

foreach (object var in mList)

{

path += "/" ;

path += var.ToString();

nodes = doc.SelectNodes(path);

if (nodes.Count == 0)

{

toAdd = true;

if (temp != null)

{

return temp.Item(0).ParentNode;

}

else

{

return doc;

}

}

temp = nodes;

// Modify mList

mList.Remove(var);

}

// Never returned

return doc;

}
Feb 19 '06 #6
PiotrKolodziej <pi*************@gmail.com> wrote:
Well, that sounds like it's a problem with the path you're using -
which unfortunately you didn't post.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Unfortunatly method is 1k lines and it's hard to get what is important.


I don't think so. Most of the method is unlikely to be relevant. The
sample program should probably:

1) Load a piece of XML from a string.
2) Select the nodes from it with a path which is also hard-coded.

I have to say, however, that any method which is 1000 lines long should
really, really be refactored. I don't like having *classes* that long
if I can avoid it...
Well. I also tought that path might me wrong. It's not.
I checked it for invalid characters and so on.
Well, there are plenty of ways in which a path can be invalid other
than invalid characters. It would really help to see it, given that the
exception sounds pretty definite in thinking it's bad.
Now i'am beginning to think that maybe i shouln't use SelectNodes method to
select nodes that doesn't exists.


No, that should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 19 '06 #7
PiotrKolodziej <pi*************@gmail.com> wrote:
Anyway here is first ver. of the method:

static private XmlNode checkEgsistance(ref ArrayList mList)


<snip>

Out of interest, why is that a "ref" parameter? I can't see anything
which modifies the value of the variable (as opposed to the object it
refers to).

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #8

Out of interest, why is that a "ref" parameter? I can't see anything
which modifies the value of the variable (as opposed to the object it
refers to).


Because i cut from the method what is not important, when i post here.
Feb 20 '06 #9
Thanks everyone. I solved the problem.
The problem was that Xpath family is not accepting white characters: in my
case - spaces.

I was forced to replace spaces with '_'.


Feb 20 '06 #10
PiotrKolodziej wrote:
Thanks everyone. I solved the problem.
The problem was that Xpath family is not accepting white characters: in my
case - spaces.

I was forced to replace spaces with '_'.


Where were you trying to use spaces? There are certain places they'll
be okay, and certain places they won't be.

Jon

Feb 20 '06 #11
XmlNodelist nodes = doc.SelectNodes(path);

Path contained spaces
Feb 20 '06 #12
PiotrKolodziej wrote:
XmlNodelist nodes = doc.SelectNodes(path);

Path contained spaces


A path expression can contain spaces, but as I said, only in certain
places. That's fine, because XML element names can't contain spaces
either, etc.

As I said before, it would have helped a lot if you'd given us an
example of the actual path that failed.

Jon

Feb 20 '06 #13
> As I said before, it would have helped a lot if you'd given us an
example of the actual path that failed.


"I Sem"

Feb 20 '06 #14
PiotrKolodziej wrote:
As I said before, it would have helped a lot if you'd given us an
example of the actual path that failed.


"I Sem"


That was the whole path? That certainly wouldn't have worked. You
wouldn't have been able to create an XML element with a space in its
name to start with for it to match.

Jon

Feb 20 '06 #15
its first directory in my directory listing.
/I Sem
Feb 20 '06 #16
PiotrKolodziej <pi*************@gmail.com> wrote:
its first directory in my directory listing.
/I Sem


But you won't have been able to create an XML element with that name,
so you couldn't possibly navigate to it in the path.

Do you see what I'm getting at? XPath expressions can certainly contain
spaces, but the element navigational parts of them can't, because XML
element names themselves can't. I'm surprised that you were able to
create an XML document to try to navigate down in the first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by pete | last post: by
4 posts views Thread by Roshawn Dawson | last post: by
3 posts views Thread by Stuart Shay | last post: by
1 post views Thread by =?Utf-8?B?RGF2aWRHQg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.