1

I'm needing to get a path say C:\SourceFiles\ from an XML File using C#.

I have been trying different escapes methods, but nothing seems to work correctly.

I have tried these:

@"C:\SourceFiles\ or C:\\SourceFiles\\ or "C:\\SourceFiles\\" and 'C:\SourceFiles'

None of these seem to work when reading from an XML file.

XmlDocument xDoc = new XmlDocument();
        xDoc.Load(strpath);

        string strsourceDirectory = xDoc.SelectSingleNode("Application/Setup/SourceDirectory").InnerText;

Here is the XML File:

<Application>
<Setup>
 <SourceDirectory>"C:\SourceFiles\"</SourceDirectory>
 <DestinationDirectory>@"C:\DestinationFiles\"</DestinationDirectory>
</Setup>

If someone has done this with C# and a XML file, please let me know how you did it.

Thanks,

1
  • Do you get an error or exception when you call xDoc.Load(strpath)? Also what is the value of strpath? Commented Nov 17, 2016 at 17:33

1 Answer 1

1

Your XML file is invalid. You need to escape the backslash \\ and close the Application tag:

<Application>
    <Setup>
        <SourceDirectory>C:\\SourceFiles\\</SourceDirectory>
        <DestinationDirectory>C:\\DestinationFiles\\</DestinationDirectory>
    </Setup>
</Application>

With this valid XML, you will be able to get the path using your code:

string strsourceDirectory =
    xDoc.SelectSingleNode("Application/Setup/SourceDirectory").InnerText;
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, looks like my XML file Code got dropped the last line during the paste operation. I do have an correct XML file with starting and ending. I also adding this to the XML file C:\\SourceFiles\\, but it gave me this C:\\\\SourceFiles\\\\, so can you do a quick check for me and see if you get different results. Thanks for answering my post.
I did recheck it and even with the extra backslashes it is now working before my code was giving me an invalid path error. Thanks for the help, it is working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.