根据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;


经过大量的Google搜索之后,我遇到了Kevin的博客并发布了一个DynamicXml对象,该对象应该可以完成工作,但是我无法使其正常工作(或者我没有正确使用它)。我对.NET的Expando对象的理解是有限的,我没有代码示例可以继续在博客文章中发表。我已经将Kevin在该主题上的两篇文章整理成一个对象,即:

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。有什么想法吗?

最佳答案

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

尝试:

payload.Name.Value

10-04 16:57