Detect xml file type without extension

S-Soft 666 Reputation points
2022-11-15T20:46:22.26+00:00

Hello
As file suggests, user can select any file, and some of them might be with no extension, I will use a content analyzer sdk to handle different situations, but there's one extension that the sdk does not support: .xml
I'm aware of using XmlDocument to .Load the .xml and check for exception, but is there another path to check the file signature/content without loading the whole file? I mean reading the mime header or something like that?
Thanks

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-11-15T22:09:06.767+00:00

    xml is just a text file. without validating, it hard to tell xml from html which uses a similar syntax (but html does not need to be valid xml). The XmlReader is probably a better approach (you would not need to read the whole file).

    0 comments No comments

  2. Fabricio Oliveira 1 Reputation point
    2022-11-15T22:29:46.353+00:00

    Hi.

    You can try the Mime Detective (https://github.com/MediatedCommunications/Mime-Detective)

    it's a nuget package to get mimetype from filecontent.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.