Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c# - Deserialize XML To Object using Dynamic

Is it possible Deserialize unknown XML to object like below?

 var xml = @"<Students><Student><Name>Arul</Name><Mark>90</Mark></Student></Students>";

 var serializer = new XmlSerializer(typeof(DynamicObject));

 dynamic students = serializer.Deserialize(new XmlTextReader(new StringReader(xml)));
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may want to try this.

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;
    }
}

--EDIT--

To make it work with xml namespaces, I added RemoveNamespaces method.

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...