Fix Error 6358 “…is not a valid style number when converting to XML” in SQL Server

If you’re getting error msg 6358 which reads something like 300 is not a valid style number when converting to XML, it’s probably because you’re trying to convert a value to XML, but the style that you’re specifying isn’t supported for conversions to that data type.

It’s not that the conversion can’t happen, it’s just that it can’t happen using the style that you’re specifying.

This can happen when using the CONVERT() function with the optional style argument. Passing an invalid style number will cause this error.

To fix this issue, make sure you use a valid style when performing the conversion.

Example of Error

Here’s an example of code that results in the error:

SELECT CONVERT( xml, '<root> <child> Value </child> </root>', 101 );

Result:

Msg 6358, Level 16, State 1, Line 1
101 is not a valid style number when converting to XML.

This error occurred because I used an invalid number for the style argument when converting the value. The style argument is the third argument.

At the time of writing the style can be in the range 0 to 3 when converting to XML.

Solution

To fix this error, pass a valid style number for the XML data type conversion.

Example:

SELECT CONVERT( xml, '<root> <child> Value </child> </root>', 3 );

Result:

<root> <child> Value </child> </root>

This time I passed a valid style argument. Passing a style of 3 preserves insignificant white space, and enables limited internal DTD subset processing.

Valid styles: