开发者

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

开发者 https://www.devze.com 2022-12-23 23:56 出处:网络
I have some xml similar to this: <?xml version=\"1.0\" encoding=\"utf-8\" ?> <data> <resources>

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

精彩评论

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