How does one test a file to see if it's a valid XML file before loading it with XDocument.Load()?

后端 未结 7 1985
挽巷
挽巷 2020-12-28 14:24

I\'m loading an XML document in my C# application with the following:

XDocument xd1 = new XDocument();
xd1 = XDocument.Load(myfile);

but be

7条回答
  •  情书的邮戳
    2020-12-28 14:40

    Just load it and catch the exception. Same for File.Exists() - the file system is volatile so just because File.Exists() returns true doesn't mean you'll be able to open it.

提交回复
热议问题