Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Escaping backslashes in XPath (C#)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chris - 30 Jun 2006 16:37 GMT
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.)
Chris Chilvers - 01 Jul 2006 02:03 GMT
>I have an XML file which have nodes that contain filepaths,
>e.g.<path>c:\SomeDirectory\SomeFile.txt</path>
[quoted text clipped - 12 lines]
>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']
Göran Andersson - 02 Jul 2006 09:43 GMT
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.

> I have an XML file which have nodes that contain filepaths,
> e.g.<path>c:\SomeDirectory\SomeFile.txt</path>
[quoted text clipped - 12 lines]
> node in question. How do I work around this? (I don't want to use double
> backslashes in the XML file.)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.