XmlDocument.SelectNodes()

P

PiotrKolodziej

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
 
M

Michael Nemtsev

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
 
P

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?

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.
 
J

Jon Skeet [C# MVP]

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

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.
 
P

PiotrKolodziej

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
 
P

PiotrKolodziej

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;

}
 
J

Jon Skeet [C# MVP]

PiotrKolodziej said:
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.
 
P

PiotrKolodziej

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.
 
P

PiotrKolodziej

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 '_'.
 
J

Jon Skeet [C# MVP]

PiotrKolodziej said:
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
 
J

Jon Skeet [C# MVP]

PiotrKolodziej said:
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
 
J

Jon Skeet [C# MVP]

PiotrKolodziej said:

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
 
J

Jon Skeet [C# MVP]

PiotrKolodziej said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top