Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq to identify the encoding declaration

Tags:

c#

xml

linq

Is there a way with Linq to XML to isolate and retrieve the attributes of the encoding declaration?

<?xml version="1.0" encoding="UTF-8" ?>

It seems like you can initalize an XDoc or XElement with the whole document or just a starting element, but how would you Isolate this tag... it doesnt have a name... or at least I dont know if it does.

like image 713
Isaiah Nelson Avatar asked May 29 '26 20:05

Isaiah Nelson


1 Answers

You can use the XDocument.Declaration Property:

XDocument document = XDocument.Load(xmlFilePath);
string encoding = document.Declaration.Encoding;

As a bonus, you can get the version number too:

string version = document.Declaration.Version;
like image 132
Bojin Li Avatar answered May 31 '26 11:05

Bojin Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!