使用动态类型的流畅XML转换

时间:2013-03-06 20:28:29

标签: c# xml dynamic

基于Kevin Hazzard的帖子; http://blogs.captechconsulting.com/blog/kevin-hazzard/fluent-xml-parsing-using-cs-dynamic-type-part-1 我正在尝试使用逻辑。基本上应该做的是获取一段XML并将其转换为可以使用的对象。例如。说我的xml是;

<someObject>
    <Name>Timmy</Name>
</someObject>

我希望能够做到;

dynamic obj = Converter(xmlText);
string name obj.Name;

经过大量谷歌搜索后,我遇到了凯文的博客并发布了一个DynamicXml对象应该可以完成这项工作,但我无法让它工作(或者我没有正确使用它)。我对.NET Expando对象的理解是有限的,我没有代码示例可以继续写博客文章。我把凯文关于这个主题的两篇文章整理成一个单独的对象,即

public class DynamicXml : DynamicObject, IEnumerable
{
    private readonly List<XElement> _elements;

    public DynamicXml(string text)
    {
        var doc = XDocument.Parse(text);
        _elements = new List<XElement> { doc.Root };
    }

    protected DynamicXml(XElement element)
    {
        _elements = new List<XElement> { element };
    }

    protected DynamicXml(IEnumerable<XElement> elements)
    {
        _elements = new List<XElement>(elements);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;

        // handle the Value and Count special cases
        if (binder.Name == "Value")
        {
            result = _elements[0].Value;
        }
        else if (binder.Name == "Count")
        {
            result = _elements.Count;
        }
        else
        {
            // try to find a named attribute first
            var attr = _elements[0].Attribute(XName.Get(binder.Name));
            if (attr != null)
            {
                // if a named attribute was found, return that NON-dynamic object
                result = attr;
            }
            else
            {
                // find the named descendants 
                var items = _elements.Descendants(
                    XName.Get(binder.Name));
                if (items != null && items.Count() > 0)
                {
                    // prepare a new dynamic object with the list of found descendants
                    result = new DynamicXml(items);
                }
            }
        }

        if (result == null)
        {
            // not found, create a new element here 
            _elements[0].AddFirst(new XElement(binder.Name));
            result = new DynamicXml(_elements[0].Descendants().First());
        }
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        if (binder.Name == "Value")
        {
            /* the Value property is the only one that may be modified. TryGetMember actually
                creates new XML elements in this implementation */
            _elements[0].Value = value.ToString();
            return true;
        }
        return false;
    }

    public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
    {
        int ndx = (int)indexes[0];
        result = new DynamicXml(_elements[ndx]);
        return true;
    }

    public IEnumerator GetEnumerator()
    {
        foreach (var element in _elements)
            yield return new DynamicXml(element);
    }
}

我的用法是;

dynamic payload = new DynamicXml(PayloadData);
Name = payload.Name;    // error thrown here

例外是;

Cannot implicitly convert type 'MyProject.Common.Exchange.DynamicXml' to 'string'

当我检查对象时,它似乎是DynamicXml的嵌套系列 - 通过查看代码的内部结构似乎是正确的。所以我想知道我实际上是如何得到Name的值的?根据博客,它应该是payload.Name。想法?

1 个答案:

答案 0 :(得分:5)

您错过了一件非常重要的事情:您可以按名称调用节点,但必须取消引用它的值,因为getMember始终返回DynamicXml对象。

尝试:

payload.Name.Value