c# - Why is a Dictionary collection that is not being iterated thru being treated as read-only? -
i trying parse contents of html table following code. issue appears when try update element in rowvals collection: tells me value read-only.
some rows span multiple columns want make sure map cell values appropriate column when parse table.
i know when collections being iterated thru, chokes when try update collection. since not iterating thru rowvals collection, rather collection, not understand why code choking when try to set value of specific kvp element in rowvals. can suggest way accomplish setting appropriate value without having add/remove existing kvps?
htmlagilitypack.htmldocument doc = new htmlagilitypack.htmldocument(); doc.load(filepath); var root = doc.documentnode.descendants().first(t => t.name == "body"); var tables = root.descendants().where(t => t.name == "table"); var trs = tables.first().elements("tr"); //parse headers var headers = trs.first().elements("th").select(t => t.innertext); //parse column values var cells = trs.skip(1).select(tr => tr.elements("td")); //update column values each row foreach (var item in cells) { int x = 0; var rowvals = headers.todictionary(t => t, t=>""); foreach (var item2 in item) { rowvals.elementat(x).value = item2.innertext; if (item2.attributes.any(t => t.name == "colspan")) { x = x + int.parse(item2.attributes.first(t => t.name == "colspan").value) - 1; } else { x++; } } }
you can never set value of keyvaluepair
because the property read only begin with. problem has nothing collection iteration.
second, dictionaries make no guarantee order of items, using .elementat()
makes no sense anyways.
Comments
Post a Comment