在C#中为编辑器加载xml混合数据 [英] Loading xml mixed data in C# for an editor

查看:67
本文介绍了在C#中为编辑器加载xml混合数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏构建世界编辑器,并且在加载地图文件的过程中需要帮助.除地图加载外,其他一切均已完成.我知道如何使用tinyxml在c ++中加载xml,但是我的c#确实很生锈.我只需要知道如何从每个元素中获取数据,以便可以在世界编辑器中使用它们.任何帮助将不胜感激.

这是我要加载到编辑器中的文件的示例.
我知道xml的布局设计很差,但是我无法修改它,因为我无法更改游戏客户端的LoadMap函数.

I am building a world editor for a game and I need help with the process of loading a map file. Everything is complete except for the map loading. I know how to load xml in c++ using tinyxml but my c# is really rusty. I just need to know how to take data from each of the elements so I can use them in the world editor. Any help would be massively appreciated.

this is an example of the file I am trying to load into my editor.
I know the layout of the xml is pretty poorly designed but I cannot modify it because I can not change the game client''s LoadMap function.

<br />
<pre lang="text"><?xml version="1.0" encoding="UTF-8"?><br />
-<Map> -<tileset progPath="../../../../../../../../Desktop/TestTileset.png" <br />
	path="Desktop/TestTileset.png" tileSetSizeY="20" tileSetSizeX="20" tileHeigh="32" tileWidth="32" GridSizeH="5" GridSizeW="5"><br />
		tilesetinfo<br />
		<tileset index="0" y="0" x="0">tile</tileset><br />
		<tileset index="0" y="1" x="0">tile</tileset><br />
		<tileset index="0" y="2" x="0">tile</tileset><br />
		<tileset index="0" y="0" x="1">tile</tileset><br />
		<tileset index="0" y="1" x="1">tile</tileset><br />
		<tileset index="0" y="2" x="1">tile</tileset><br />
</tileset> <br />
 <item y="36" x="40" lane="2">waypoint</item><br />
 <item y="116" x="23" lane="2">waypoint</item><br />
 <item y="116" x="23" lane="2">node</item><br />
 <item y="120" x="72" lane="2">waypoint</item><br />
 <item y="120" x="72" lane="2">node</item><br />
 <item y="114" x="120" lane="2">waypoint</item><br />
 <item y="114" x="120" lane="2">node</item><br />
 <item y="70" x="138" lane="2">waypoint</item><br />
 <item y="116" x="23" lane="2" owner="1">tower</item><br />
 <item y="114" x="120" lane="2" owner="2">tower</item><br />
 <item y="30" x="117" owner="1">avatar</item><br />
 <item y="126" x="125" owner="2">avatar</item><br />
</Map></pre>

推荐答案

这是我对可以使用的内容的简短概述:

This is my short overview of what you can use:


  1. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见
  2. 使用类System.Xml.XmlTextReader; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ ].
  3. 使用类System.Xml.XmlTextReader;这是最快的读取方法,尤其是您需要跳过一些数据.
    请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].



从您的问题中我看不出您是否可以自己定义文件格式.如果可能的话,这似乎很有可能,您不必直接在应用程序级别使用XML.您应该使用序列化从持久性中提取数据模型.那我建议只使用数据合同:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

请同时参阅我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [



I don''t clearly see from your question if you can define the file format by yourself or not. If you can, which seems likely, you don''t have to work with XML directly at the level of the application. You should abstract out the data model from its persistence using serialization. Then I would advise to work only with the Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于在C#中为编辑器加载xml混合数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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