-3

I have a path starting with the common C:\Users...\myXmlFile.xml and I am trying to load it with the

XElement.Load(@"C:\Users\...\myXmlFile.xml") 

but it doesn load it and it says:

Data at the root level is invalid. Line 1, position 1.

I found out what's the reason and I can not solve it.The thing is that in some of the folders that I have in the path there is a folder with spaces and dashes between the words. Example C:\users\my - folder - doesnt - work\otherFolder\myXmlFile.xml

it works when I remove the spaces.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
mathinvalidnik
  • 1,566
  • 10
  • 35
  • 57

2 Answers2

2

Spaces and dashes in path do not affect xml loading. If there would be some problem with finding file, you'd see FileNotFoundException. Thus error says there is wrong data in first position of first line, I think your xml file even do not have tag (which should start from < symbol) at the beginning of file.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Try,

FileInfo file = new FileInfo(@"C:\Users\...\myXmlFile.xml");
if(file.Exists)
{
   XElement root = XElement.Load(file.Fullname);
   ...
}
else
   throw new FileNotFoundException(file.Fullname);
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69