2

I have XML content in a string that defines its encoding in its declaration. I want to get a byte array from that string and download it to a client browser.

The following works, but I'm not that experienced with encoding and am wondering will I cause something to blow up if I do it this way?

Basically, I'm getting the encoding from the declaration, and using that encoding to convert the XML string to a byte array. Is that how it should work?

var xdoc = XDocument.Parse(xmlString);
var encoding = System.Text.Encoding.GetEncoding(xdoc.Document.Declaration.Encoding);

var encoded = encoding.GetBytes(xmlString);

Response.AppendHeader("Content-Disposition", "attachment; filename=" + xmlData.FileName);
return File(encoded, "text/plain");

1 Answer 1

1

You should put an exception handler around GetEncoding - it'll throw if it doesn't recognise the encoding name. Otherwise you're fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.