【问题标题】:Extracting certain XML element values from different parent elements从不同的父元素中提取某些 XML 元素值
【发布时间】: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 对象的问题。您的问题并未表明您进行了哪些研究或您为自己解决这个问题付出了哪些努力。

标签: c# xml


【解决方案1】:

问题是使用列表索引器尝试添加新值。您可以验证这是否行不通,而无需担心 XML:

using System;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        var list = new List<string>();
        list[0] = "test"; // Bang: ArgumentOutOfRangeException
    }
}

幸运的是,您根本不需要它 - 您的代码可以更正并简化为:

XDocument xml = XDocument.Load("C:\\Users\\Test.xml");
List<string> vertices = xml
    .Descendants("Vertex")
    .Select(x => x.Attribute("position").Value)
    .ToList();

【讨论】:

  • 如何区分属于 的顶点和属于 的顶点?
  • @Jellyracecar:您没有显示任何&lt;Wall&gt;&lt;Cube&gt; 元素(它们只是Mesh 中的id 属性),因此很难提供任何信息。我建议您使用适当的 XML 提出一个新问题,您尝试过什么等。如果您实际上是指 ID,只需使用 xml.Root.Attribute("id").Value 查找 id 属性的值。
【解决方案2】:

使用 Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication106
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Mesh> mesh = doc.Descendants("Mesh").Select(x => new Mesh()
            {
                id = (string)x.Attribute("id"),
                vertexes = x.Elements("Vertex").Select(y => ((string)y.Attribute("position")).Split(new char[] {','}).Select(z => decimal.Parse(z)).ToList()).ToList(),
                faces = x.Elements("Face").Select(y => ((string)y.Attribute("vertices")).Split(new char[] { ',' }).Select(z => int.Parse(z)).ToList()).ToList(),

            }).ToList();
        }

    }
    public class Mesh
    {
        public string id { get; set; }
        public List<List<decimal>> vertexes { get; set; }
        public List<List<int>> faces { get; set; }


    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多