Home All Groups Group Topic Archive Search About

Escaping backslashes in XPath (C#)

Author
30 Jun 2006 3:37 PM
chris
I have an XML file which have nodes that contain filepaths,
e.g.<path>c:\SomeDirectory\SomeFile.txt</path>

I'm using an XmlDocument and XPath query to try and find a node
XmlDocument doc = new XmlDocument();
doc.LoadXml(File.ReadAllText(xmlFilePath));

string somePath = @"c:\SomeDirectory\SomeFile.txt";
string query = "node1/node2[Path='" + somePath + "']";
XmlNodeList nodes = doc.SelectNodes(query);

The problem is, query becomes
"node1/node2[Path='c:\\SomeDirectory\\SomeFile.txt']" (Note the double quotes
due to the escaping of the backslashes). Obviously this doesn't match the XML
node in question. How do I work around this? (I don't want to use double
backslashes in the XML file.)

Author
1 Jul 2006 1:03 AM
Chris Chilvers
On Fri, 30 Jun 2006 08:37:02 -0700, chris
<ch***@discussions.microsoft.com> wrote:

Show quote
>I have an XML file which have nodes that contain filepaths,
>e.g.<path>c:\SomeDirectory\SomeFile.txt</path>
>
>I'm using an XmlDocument and XPath query to try and find a node
>XmlDocument doc = new XmlDocument();
>doc.LoadXml(File.ReadAllText(xmlFilePath));
>
>string somePath = @"c:\SomeDirectory\SomeFile.txt";
>string query = "node1/node2[Path='" + somePath + "']";
>XmlNodeList nodes = doc.SelectNodes(query);
>
>The problem is, query becomes
>"node1/node2[Path='c:\\SomeDirectory\\SomeFile.txt']" (Note the double quotes
>due to the escaping of the backslashes). Obviously this doesn't match the XML
>node in question. How do I work around this? (I don't want to use double
>backslashes in the XML file.)

I'm not sure what your problem is here. Unless you're doing something
else to somePath then it should not have double backslashes. Any escape
characters are resolved when the application is compiled.

==========

using System;

namespace Test {
    public class Test {
        public static void Main() {
            string somePath = @"c:\SomeDirectory\SomeFile.txt";
            string query = "node1/node2[Path='" + somePath + "']";

            Console.WriteLine(query);
            Console.ReadKey();
        }
    }
}

==========

Outputs:
node1/node2[Path='c:\SomeDirectory\SomeFile.txt']
Author
2 Jul 2006 8:43 AM
Göran Andersson
Why do you think that the query looks like that? Do you look at the
contenst of the variable using the debug mode in Visual Studio? Then
it's shown as it would be written in code, so the double backslashes
means a single backslash.

chris wrote:
Show quote
> I have an XML file which have nodes that contain filepaths,
> e.g.<path>c:\SomeDirectory\SomeFile.txt</path>
>
> I'm using an XmlDocument and XPath query to try and find a node
> XmlDocument doc = new XmlDocument();
> doc.LoadXml(File.ReadAllText(xmlFilePath));
>
> string somePath = @"c:\SomeDirectory\SomeFile.txt";
> string query = "node1/node2[Path='" + somePath + "']";
> XmlNodeList nodes = doc.SelectNodes(query);
>
> The problem is, query becomes
> "node1/node2[Path='c:\\SomeDirectory\\SomeFile.txt']" (Note the double quotes
> due to the escaping of the backslashes). Obviously this doesn't match the XML
> node in question. How do I work around this? (I don't want to use double
> backslashes in the XML file.)

AddThis Social Bookmark Button