Find the error:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement root = doc.DocumentElement;

if ( doc == null )
{
     throw new InvalidDataException("No valid xml");
}

    .............