Désérialiser XML à un object en utilisant Dynamic

Est-il possible de désérialiser XML inconnu pour object comme ci-dessous?

var xml = @"Arul90"; var serializer = new XmlSerializer(typeof(DynamicObject)); dynamic students = serializer.Deserialize(new XmlTextReader(new SsortingngReader(xml))); 

    Vous voudrez peut-être essayer ceci.

     ssortingng xml = @"  Arul 90   Arul2 80  "; dynamic students = DynamicXml.Parse(xml); var id = students.Student[0].ID; var name1 = students.Student[1].Name; foreach(var std in students.Student) { Console.WriteLine(std.Mark); } 

     public class DynamicXml : DynamicObject { XElement _root; private DynamicXml(XElement root) { _root = root; } public static DynamicXml Parse(ssortingng xmlSsortingng) { return new DynamicXml(XDocument.Parse(xmlSsortingng).Root); } public static DynamicXml Load(ssortingng filename) { return new DynamicXml(XDocument.Load(filename).Root); } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = null; var att = _root.Atsortingbute(binder.Name); if (att != null) { result = att.Value; return true; } var nodes = _root.Elements(binder.Name); if (nodes.Count() > 1) { result = nodes.Select(n => n.HasElements ? (object)new DynamicXml(n) : n.Value).ToList(); return true; } var node = _root.Element(binder.Name); if (node != null) { result = node.HasElements || node.HasAtsortingbutes ? (object)new DynamicXml(node) : node.Value; return true; } return true; } } 

    –MODIFIER–

    Pour le faire fonctionner avec les espaces de noms xml, j’ai ajouté la méthode RemoveNamespaces .

     public class DynamicXml : DynamicObject { XElement _root; private DynamicXml(XElement root) { _root = root; } public static DynamicXml Parse(ssortingng xmlSsortingng) { return new DynamicXml(RemoveNamespaces(XDocument.Parse(xmlSsortingng).Root)); } public static DynamicXml Load(ssortingng filename) { return new DynamicXml(RemoveNamespaces(XDocument.Load(filename).Root)); } private static XElement RemoveNamespaces(XElement xElem) { var attrs = xElem.Atsortingbutes() .Where(a => !a.IsNamespaceDeclaration) .Select(a => new XAtsortingbute(a.Name.LocalName, a.Value)) .ToList(); if (!xElem.HasElements) { XElement xElement = new XElement(xElem.Name.LocalName, attrs); xElement.Value = xElem.Value; return xElement; } var newXElem = new XElement(xElem.Name.LocalName, xElem.Elements().Select(e => RemoveNamespaces(e))); newXElem.Add(attrs); return newXElem; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = null; var att = _root.Atsortingbute(binder.Name); if (att != null) { result = att.Value; return true; } var nodes = _root.Elements(binder.Name); if (nodes.Count() > 1) { result = nodes.Select(n => n.HasElements ? (object)new DynamicXml(n) : n.Value).ToList(); return true; } var node = _root.Element(binder.Name); if (node != null) { result = node.HasElements || node.HasAtsortingbutes ? (object)new DynamicXml(node) : node.Value; return true; } return true; } }