简体   繁体   English

将此XML文件解析为Dictionary?

[英]Parsing this XML file to a Dictionary?

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. 我是XML序列化的新手,到目前为止我一直在使用XDocument并迭代XML文件来加载东西。 I know there's a way to serialize XML files, recently I came across the following bit of XML: 我知道有一种方法可以序列化XML文件,最近我遇到了以下一些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. 并且一直在试图弄清楚我是如何加载它并将其解析成字典,顶部说明资产类型= System.collections.generic.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? 我可以轻松遍历xml结构并将键与值匹配,但看起来会有更好的方法吗?

EDIT: Having just skimmed this article and this one , I suspect you want to use either IntermediateSerializer or XmlImporter . 编辑:刚走脱脂这篇文章这一个 ,我怀疑你想使用任何IntermediateSerializerXmlImporter

Not being an XNA developer, it's unclear to me how it all fits together, but hopefully that's enough to get you started. 不是XNA开发人员,我不清楚它是如何组合在一起的,但希望这足以让你开始。

Of course, if this was just an example of XML and you're actually wanting to serialize data that isn't part of XNA, you may want to look at XmlSerializer . 当然,如果这只是XML的一个例子 ,并且您实际上想要序列化不属于XNA的数据,那么您可能需要查看XmlSerializer Personally I've found it simpler to control the serialization manually using LINQ to XML - it's easier to manage different versions etc that way - but in some cases XmlSerializer would be more convenient. 就个人而言,我发现使用LINQ to XML手动控制序列化更简单 - 以这种方式管理不同版本等更容易 - 但在某些情况下XmlSerializer会更方便。

It's surprisingly straight forward. 这是令人惊讶的直截了当。 Add the XML file to your content project . 将XML文件添加到内容项目中 Keep the default importer ("XML Content") and processor (none). 保留默认导入程序(“XML内容”)和处理程序(无)。

The XNA content pipeline will build it into an XNB file for you. XNA内容管道将为您构建一个XNB文件。

Then simply load it with ContentManager.Load like you would any other asset: 然后像使用任何其他资产一样使用ContentManager.Load加载它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM