将此XML文件解析为词典? [英] Parsing this XML file to a Dictionary?

查看:186
本文介绍了将此XML文件解析为词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的XML序列化,到目前为止,我一直在使用XDocument和迭代XML文件来加载东西。我知道有一种序列化XML文件的方法,最近我遇到了以下XML:

 <?xml version = 1.0encoding =utf-8?> 
< XnaContent>
<资产类型=System.Collections.Generic.Dictionary [System.String,Microsoft.Xna.Framework.Rectangle]>
< Item>< Key> frame0< / Key>< Value> 0 0 128 128< / Value>< / Item>
< Item>< Key> frame1< / Key>< Value> 128 0 128 128< / Value>< / Item>
< / Asset>
< / XnaContent>

而且一直试图找出我如何加载它并将其解析成字典,表示资产类型的顶部= System.collections.generic.dictionary似乎建议我应该能够快速将其转换为字典。



所以我的问题是如何是我做的?我可以轻松地遍历xml结构并将键匹配到值,但是看起来会有更好的方法来实现?

解决方案

这是令人惊讶的直接前进。 将XML文件添加到您的内容项目。保留默认进口商(XML内容)和处理器(无)。



XNA内容管道将构建为 XNB 文件。



然后简单地加载它与 ContentManager.Load 就像你任何其他资产:

  var myContent = Content.Load<字典< string,Rectangle>>(MyXmlFile); 


I'm very new to XML serialization, up until now I've been using XDocument and iterating through an XML file to load stuff up. I know there's a way to serialize XML files, recently I came across the following bit of XML:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<Asset Type="System.Collections.Generic.Dictionary[System.String, Microsoft.Xna.Framework.Rectangle]">
<Item><Key>frame0</Key><Value>0 0 128 128</Value></Item>
<Item><Key>frame1</Key><Value>128 0 128 128</Value></Item>
</Asset>
</XnaContent>

And have been trying to figure out how I'd load it up and parse it into a dictionary, the top part stating asset type=System.collections.generic.dictionary seems to suggest I should be able to quickly convert this into a dictionary.

So my question is how would I do that? I could easily iterate through the xml structure and match up keys to values but it looks like there would be a better way to do it?

解决方案

It's surprisingly straight forward. Add the XML file to your content project. Keep the default importer ("XML Content") and processor (none).

The XNA content pipeline will build it into an XNB file for you.

Then simply load it with ContentManager.Load like you would any other asset:

var myContent = Content.Load<Dictionary<string, Rectangle>>("MyXmlFile");

这篇关于将此XML文件解析为词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆