user2763048
user2763048

Reputation: 11

Get path from XML File using C#

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,

Upvotes: 1

Views: 1559

Answers (1)

mtheriault
mtheriault

Reputation: 1075

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;

Upvotes: 1

Related Questions