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

后端 未结 7 1984
挽巷
挽巷 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:53

    It's probably just worth catching the specific exception if you want to show a message to the user:

     try
     {
       XDocument xd1 = new XDocument();
       xd1 = XDocument.Load(myfile);
     }
     catch (XmlException exception)
     {
         ShowMessage("Your XML was probably bad...");
     }
    

提交回复
热议问题