使用动态反序列化XML到对象

使用动态反序列化XML到对象,第1张

使用动态反序列化XML到对象

您可能想尝试一下。

string xml = @"<Students>     <Student ID=""100"">         <Name>Arul</Name>         <Mark>90</Mark>     </Student>     <Student>         <Name>Arul2</Name>         <Mark>80</Mark>     </Student> </Students>";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(string xmlString)    {        return new DynamicXml(Xdocument.Parse(xmlString).Root);    }    public static DynamicXml Load(string filename)    {        return new DynamicXml(Xdocument.Load(filename).Root);    }    public override bool TryGetMember(GetMemberBinder binder, out object result)    {        result = null;        var att = _root.Attribute(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.HasAttributes ? (object)new DynamicXml(node) : node.Value; return true;        }        return true;    }}

- 编辑 -

为了使其与xml名称空间一起使用,我添加RemoveNamespaces 方法。

public class DynamicXml : DynamicObject{    XElement _root;    private DynamicXml(XElement root)    {        _root = root;    }    public static DynamicXml Parse(string xmlString)    {        return new DynamicXml(RemoveNamespaces(Xdocument.Parse(xmlString).Root));    }    public static DynamicXml Load(string filename)    {        return new DynamicXml(RemoveNamespaces(Xdocument.Load(filename).Root));    }    private static XElement RemoveNamespaces(XElement xElem)    {        var attrs = xElem.Attributes()         .Where(a => !a.IsNamespaceDeclaration)         .Select(a => new XAttribute(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.Attribute(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.HasAttributes ? (object)new DynamicXml(node) : node.Value; return true;        }        return true;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5005520.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存