3
我有以下XML:如何通過LINQ創建詞典<int, string>到XML?
<FootNotes>
<Line id="10306" reference="*"></Line>
<Line id="10308" reference="**"></Line>
<Line id="10309" reference="***"></Line>
<Line id="10310" reference="****"></Line>
<Line id="10311" reference="+"></Line>
</FootNotes>
和我有下面的代碼,我得到一個Dictionary<int, string>()
對象爲
myObject.FootNotes
使每一行是鍵/值對
var doc = XElement.Parse(xmlString);
var myObject = new
{
FootNotes = (from fn in doc
.Elements("FootNotes")
.Elements("Line")
.ToDictionary
(
column => (int) column.Attribute("id"),
column => (string) column.Attribute("reference")
)
)
};
我不確定如何從XML到對象中。任何人都可以提出解決方案
該代碼是_almost_正確的,除了'(從fn in和尾部')'。您在編輯之前提供的示例指出了這一點。你應該恢復最後一次編輯,所以我可以選擇這個作爲正確的答案 – DaveDev 2010-05-13 16:25:47
@DaveDev:我其實現在也添加了:) – 2010-05-13 16:27:41