Detect xml file type without extension

S-Soft 646 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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,219 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,366 Reputation points
    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