3

我的 DynamicXML 对象有一些问题,它解析 XML 很好,并且对于单个元素效果很好,但是当我有多个子元素时它会倒下。我显然错过了一些东西,但我不知道是什么。

这是课程:

public class DynamicXml : DynamicObject
{
    readonly XElement element;

    public DynamicXml(string xml)
    {
        element = XElement.Parse(xml);
    }

    public DynamicXml(XElement xElement)
    {
        element = xElement;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (element == null)
        {
            result = null;
            return false;
        }

        var sub = element.Element(binder.Name);

        if (sub == null)
        {
            result = null;
            return false;
        }
        else
        {
            result = new DynamicXml(sub);
            return true;
        }
    }

    public override string ToString()
    {
        return element != null ? element.Value : string.Empty;
    }

    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        try
        {
            result = Convert.ChangeType(element.Value, binder.ReturnType);
            return true;
        }
        catch { }

        return base.TryConvert(binder, out result);
    }
}

这就是我将如何使用它

var response = "<response><foo>some xml here</foo></response>"
result = new DynamicXml(response);

var newObj = new SumObject();
newObj.Foo = result.foo;

现在这工作正常,但如果我的 xml 是:

<response>
   <foo>
     <bahs>
        <bah>value here</bah>
        <bah>value here</bah>
        <bah>value here</bah>
     </bahs>
   </foo>
<response>

我想使用类似的东西:

for(var bahs in result.foo.bahs)
{
   //magic code here
}

但这不是正在发生的事情我只是得到一个错误:

Cannot implicitly convert type 'DynamicXml' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

非常感谢您的帮助。

4

1 回答 1

3

我想出了解决方案,我需要实现 IEnumerable 和 GetEnumerator()

这是解决方案类:

    public class DynamicXml : DynamicObject, IEnumerable
    {
        readonly XElement element;

        public DynamicXml(string xml)
        {
            element = XElement.Parse(xml);
        }

        public DynamicXml(XElement xElement)
        {
            element = xElement;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (element == null)
            {
                result = null;
                return false;
            }

            var sub = element.Element(binder.Name);

            if (sub == null)
            {
                result = null;
                return false;
            }
            else
            {
                result = new DynamicXml(sub);
                return true;
            }
        }

        public override string ToString()
        {
            return element != null ? element.Value : string.Empty;
        }

        public IEnumerator GetEnumerator()
        {
            return element.Elements().Select(child => new DynamicXml(child)).GetEnumerator();
        }

        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            try
            {
                result = Convert.ChangeType(element.Value, binder.ReturnType);
                return true;
            }
            catch { }

            return base.TryConvert(binder, out result);
        }
    }
于 2012-08-10T13:25:10.760 回答