开发者

linq to xml. read. and assign to ViewData..noob

I have some xml similar to this:

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <resources>
        <resource key="Title">Alpha</resource>
        <resource key="ImageName">Small.png</r开发者_StackOverflow中文版esource>
        <resource key="Desc">blah</resource>
</resources>
</data>

using linq-xml how can i assign each resource here as a key value pair with the ViewData collection.

Thanks.


var doc = XDocument.Parse(documentString);
foreach (var res in doc.Root.Descendants("resources")) {
    ViewData[(string) res.Attribute("key")] = res.Value;
}

Should work.


assuming you loadt hat xml into an XDocument, you can just iterate on teh descendants. here's a quick example, if it's coming from a string:

var doc = XDocument.Parse(docAsString);
 foreach (var resource in doc.Descendants("resource"))
     ViewData[resource.Attribute("key").Value] = resource.Value;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜