using System.Collections.Generic;
public static XDocument ConvertStreamToXml(Stream stream)
XmlReaderSettings settings = new XmlReaderSettings
DtdProcessing = DtdProcessing.Ignore
var doc = XDocument.Load(XmlReader.Create(stream, settings));
return new XDocument(new XElement(doc.Root));
public static object ConvertXmlToDic(XElement element)
var result = element.Elements().
AnnotationValue = (e.Descendants().Count() == 0) ? e.Value : ConvertXmlToDic(e)
).ToDictionary(e => e.Annotation, e => e.AnnotationValue);
public static Dictionary<XName, object> ConvertXMLStringToObject(String xmlData)
var doc = XDocument.Parse(xmlData);
var xDocument = new XDocument(new XElement(doc.Root));
var xElement = xDocument.Root;
var result = xElement.Elements().
AnnotationValue = (e.Descendants().Count() == 0) ? System.Security.SecurityElement.Escape(e.Value) : ConvertXmlToDic(e)
).ToDictionary(e => e.Annotation, e => e.AnnotationValue);
public static void Main()
string str = "<Root><DocumentUpload><CorrespondenceID /><Version>1.0</Version><SourceSystemID>cdis</SourceSystemID><FileName>TES&T.txt</FileName></DocumentUpload></Root>";
ConvertXMLStringToObject(XDocument.Load(new MemoryStream(System.Text.Encoding.ASCII.GetBytes(System.Security.SecurityElement.Escape(str)))).ToString());