我試圖使用Fluent接口解析XML,如here所述。爲使用集合的通用列表使用構造函數時出錯
這個例子很好,但給我一個編譯器錯誤,我無法修復。
的錯誤是在下面的受保護的構造:
protected DynamicXml(IEnumerable<XElement> elements)
{
_elements = new List<XElement>(elements); // error on this line
}
兩個編譯器錯誤,我就在這行是:
Argument 1: cannot convert from
'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to
'System.Xml.Linq.XElement'
和
The best overloaded Add method
'System.Collections.Generic.List<System.Xml.Linq.XElement>.
Add(System.Xml.Linq.XElement)' for the collection initializer
has some invalid arguments
我有複製/粘貼以下完整代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Dynamic;
using System.Xml.Linq;
using System.Collections;
namespace FluentXML
{
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> iteratorElem)
{
_elements = new List<XElement> { iteratorElem };
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
if (binder.Name == "Value")
result = _elements[0].Value;
else if (binder.Name == "Count")
result = _elements.Count;
else
{
var attr = _elements[0].Attribute(
XName.Get(binder.Name));
if (attr != null)
result = attr;
else
{
var items = _elements.Descendants(
XName.Get(binder.Name));
if (items == null || items.Count() == 0)
return false;
result = new DynamicXml(items);
}
}
return true;
}
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);
}
}
}
namespace FluentXML
{
class Program
{
static void Main(string[] args)
{
string xmlString = FluentXML.Properties.Resources.InputString;
dynamic dx = new DynamicXml(xmlString);
Console.WriteLine("PublicationDate='{0}'", dx.pubdate.Value);
Console.WriteLine("BookCount='{0}'", dx.book.Count);
foreach (dynamic d in dx.book)
{
Console.WriteLine("----- Begin Book -----");
Console.WriteLine("Price='{0}'", d.price.Value);
Console.WriteLine("Title='{0}'", d.title.Value);
Console.WriteLine("AuthorCount='{0}'", d.authors.author.Count);
foreach (dynamic a in d.authors.author)
{
Console.WriteLine(" ---- Begin Author ----");
Console.WriteLine(" EmailAddress='{0}'", a.email.address.Value);
Console.WriteLine(" FirstName='{0}'", a.name.first.Value);
Console.WriteLine(" MiddleName='{0}'", a.name.middle.Value);
Console.WriteLine(" LastName='{0}'", a.name.last.Value);
Console.WriteLine(" ----- End Author -----");
}
Console.WriteLine("------ End Book ------");
}
}
}
}
標題有點令人困惑 - 我不認爲你的問題與動態編程有關,是嗎? – Niki 2009-12-22 13:47:54
Yeh這是令人困惑的,我最好改變它,希望能獲得更多的意見 – ardsrk 2009-12-22 13:49:37
「C#程序設計問題」也過於籠統了 – 2009-12-22 13:54:22