public static (bool, XmlException?) IsvalidXml(string xml)
using var reader = new StringReader(xml);
using var xmlReader = XmlReader.Create(reader, new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Auto });
while (xmlReader.Read());
catch (XmlException exception)
return (false, exception);
static void Main(string[] args) {
var (v, e) = IsvalidXml("<Tag>x=y&z=w</Tag>");
Console.WriteLine(v ? "true" : "false");
Console.WriteLine(e?.Message);