基于 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;
经过多次谷歌搜索,我发现了 Kevin 的博客并发布了一个DynamicXml
应该完成这项工作但我无法让它工作的对象(或者我没有正确使用它)。我对 .NETExpando
对象的理解是有限的,并且我没有代码示例可以在博客文章中继续。我已将凯文关于该主题的两个帖子整理成一个对象,即;
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
。想法?