I am currently trying to make an importer for collada(.dae) files, which are based on XML. I have the xml file deserialized into objects that I can easily access. For example, the .dae file may have a setup like this...
我目前正在尝试为基于XML的collada(.dae)文件创建一个导入程序。我将xml文件反序列化为我可以轻松访问的对象。例如,.dae文件可能有这样的设置......
<library_geometries>
<geometry id="Cube1s_008-mesh" name="Cube1s.008">
<mesh>
<source id="Cube1s_008-mesh-positions">
<float_array ...... />
</source>
<source id="Cube1s_008-mesh-normals">
</source>
<vertices id="Cube1s_008-mesh-vertices">
<input semantic="POSITION" source="#Cube1s_008-mesh-positions"/>
</vertices>
</mesh>
</geometry>
<geometry>
....
</geometry>
</library_geometries>
It is then deserialized (with xmlSerializer.Deserialize) in a similar fashion. So to access the second "source" in "mesh" I would do library_geometries.geometry[0].mesh.source[1];
然后以类似的方式对其进行反序列化(使用xmlSerializer.Deserialize)。所以要访问“网格”中的第二个“源”,我会做library_geometries.geometry [0] .mesh.source [1];
All is good there, the problem I am running into is easily traversing this deserialized xml document.
一切都很好,我遇到的问题很容易遍历这个反序列化的xml文档。
For example, in "vertices" there is a source="#Cube1s_008-mesh-positions" which basically means "to access the vertices data, go to the source with an id of "Cube1s_008-mesh-positions"". What I want to do is to easily go from the vertices directly to the source, or anything, that has that id. So it would look something like library_geometries.geometry[0].mesh.vertices.GoToSource(); and with that I can do library_geometries.geometry[0].mesh.vertices.GoToSource().float_array.values;
例如,在“顶点”中有一个source =“#Cube1s_008-mesh-positions”,它基本上意味着“访问顶点数据,转到id为”Cube1s_008-mesh-positions“”的源。我想要做的是轻松地从顶点直接转到具有该id的源或任何东西。所以它看起来像library_geometries.geometry [0] .mesh.vertices.GoToSource();并且我可以做library_geometries.geometry [0] .mesh.vertices.GoToSource()。float_array.values;
I am assuming I would need to do this with reflection. Perhaps search for any field that has [XmlAttribute("id")] and then somehow return its object in its proper type. Any ideas are appreciated.
我假设我需要用反射来做这件事。也许搜索具有[XmlAttribute(“id”)]的任何字段,然后以某种方式返回其正确类型的对象。任何想法都表示赞赏。
2 个解决方案
#1
0
There are two basic methods you can use.
您可以使用两种基本方法。
1) Build a dictionary
2) Add a new property Geometry geometry to the class veritices which is NonSerialize. After deserialize, enumerate through the classes adding the value to the property geometry.
#2
0
That is why I like dictionaries. I often use classes like the one below for nested structures.
这就是我喜欢词典的原因。我经常使用类似下面的类来嵌套结构。
public class Node
{
public string name { get; set; }
public List<Node> children { get; set; }
public Dictionary<string, string> dict { get; set; }
}
#1
0
There are two basic methods you can use.
您可以使用两种基本方法。
1) Build a dictionary
2) Add a new property Geometry geometry to the class veritices which is NonSerialize. After deserialize, enumerate through the classes adding the value to the property geometry.
#2
0
That is why I like dictionaries. I often use classes like the one below for nested structures.
这就是我喜欢词典的原因。我经常使用类似下面的类来嵌套结构。
public class Node
{
public string name { get; set; }
public List<Node> children { get; set; }
public Dictionary<string, string> dict { get; set; }
}