【发布时间】:2019-03-21 13:32:08
【问题描述】:
我有一个 XML 文件,其中包含 10 个 Mesh 节点,每个节点都包含 Vertex 和 Face 元素。基本上我需要为每个 Mesh 节点创建一个:
- 包含所有 Vertex(vector3 类型)的新列表
- 包含所有面孔的新列表(vector3 类型)
- 保存网格 ID(字符串类型)
我不知道使用什么语句来进行这种动态信息分析和提取。下面是一些简化的 XML 代码用于说明。
<Mesh id="Cube">
<Vertex position="0.9823, 2.3545, 30.251" />
<Vertex position="-0.0177, 2.3545, 30.251" />
<Vertex position="0.9823, 3.3545, 30.251" />
<Vertex position="-0.0177, 3.3545, 30.251" />
<Face vertices="0, 2, 3" />
<Face vertices="0, 3, 1" />
<Mesh id="Wall">
<Vertex position="-4.9048, -1.0443, -4.8548" />
<Vertex position="-5.404, -1.018, -4.8636" />
<Vertex position="-4.6416, 3.9487, -4.8548" />
<Vertex position="-5.1409, 3.975, -4.8636" />
<Face vertices="0, 2, 3" />
<Face vertices="0, 3, 1" />
我当前的解决方案返回“参数超出范围”。我不确定如何将 Vertices 列表转换为 Vector3 列表以及如何首先检索网格 ID。
XDocument xml = XDocument.Load("C:\\Users\\Test.xml");
List<string> Vertices= new List<string>();
int i = 0;
IEnumerable<XElement> de =
from element in xml.Descendants("Vertex")
select element;
foreach (XElement element in de)
{
Vertices[i] = element.Attribute("position").Value;
i += 1;
}
【问题讨论】:
-
你看过 XSLT 吗?
-
仅 XML 本身就使问题过于宽泛。发布您正在尝试的 C# 代码。
-
有很多关于将 XML 反序列化为 .Net 对象的问题。您的问题并未表明您进行了哪些研究或您为自己解决这个问题付出了哪些努力。