title: Deadlock graph from XEvents doesn't produce "good" XML
body: <p>I'm investigating a deadlock and used the following query (which I got from Jonathan Kehayias's Pluralsight course on deadlocks) to extract information from extended events regarding the deadlock:</p>
  
  <pre><code>SELECT  
      XEvent.query('data[@name="xml_report"]/value/deadlock') AS deadlock_graph 
  FROM    (SELECT CAST([target_data] AS XML) AS TargetData 
           FROM sys.dm_xe_session_targets AS st 
           INNER JOIN sys.dm_xe_sessions AS s  
              ON [s].[address] = [st].[event_session_address] 
           WHERE [s].[name] = N'system_health' 
             AND [st].[target_name] = N'ring_buffer') AS Data 
  CROSS APPLY TargetData.nodes ('RingBufferTarget/event[@name="xml_deadlock_report"]') AS XEventData (XEvent); 
  </code></pre>
  
  <p>Strangely that gave me a 1-row, 1-column resultset, containing an empty value. After a bit of digging I found out that it was due to the deadlock graph portion of the XML document that gets returned by the inner query wasn't valid XML. It looked like this:</p>
  
  <p><img src="https://i.stack.imgur.com/RvcQf.png" alt="malformed XML"></p>
  
  <p>As you can see the angle brackets have all been replaced by their HTML encoding. I don't understand how this happened because all I'm doing is querying the database directly from SSMS.</p>
  
  <p>Anyway, once I realised the problem I was able to replace all the erroneous values with the proper angle brackets and my XML was valid, and I was able to examine the deadlock. I find this very strange though, does anyone know why the deadlock graph is coming back like this?</p>
  
  <p>I should note that the rest of the XML (i.e. the rest of the events other than the deadlock graph) came back perfectly formed as you can see here:
  <img src="https://i.stack.imgur.com/2EY5X.png" alt="enter image description here"></p>
  
  <p>Here's my version info:</p>
  
  <p>Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)   Jun 28 2012 08:36:30   Copyright (c) Microsoft Corporation  Enterprise Edition (64-bit) on Windows NT 6.1  (Build 7601: Service Pack 1) (Hypervisor) </p>
  
  <p>Anyone have an idea as t what is going on? I find it rather peculiar.</p>
  
  <p>thanks in advance</p>
  
  <p>Jamie</p>