C# 解析高级XML文件&;用C语言将信息转化为类# 需要做什么

C# 解析高级XML文件&;用C语言将信息转化为类# 需要做什么,c#,xml,C#,Xml,目前我有一个高级的XML文件需要解析。我需要遍历该文件并分别读取每个“实体”标记。尽管我遇到的问题是阅读和迭代统计数据和插槽。此外,Stat和Slot标记的数量也会因实体而异。(是的,我已经研究过这个话题,但是我仍然找不到一个不产生错误的方法,因为我需要更多的指导。其他帖子还没有得到我所希望的精确修复…) XML文件 2. 1. 120 0.1 10 人体躯干 4. 2. 15 120 0.1 人体躯干 6. 4. 25 120 0.1 人体躯干 如果有人对这篇文章有任何批评,请说出来,我将

目前我有一个高级的XML文件需要解析。我需要遍历该文件并分别读取每个“实体”标记。尽管我遇到的问题是阅读和迭代统计数据和插槽。此外,Stat和Slot标记的数量也会因实体而异。(是的,我已经研究过这个话题,但是我仍然找不到一个不产生错误的方法,因为我需要更多的指导。其他帖子还没有得到我所希望的精确修复…)

XML文件

2.
1.
120
0.1
10
人体躯干
4.
2.
15
120
0.1
人体躯干
6.
4.
25
120
0.1
人体躯干
如果有人对这篇文章有任何批评,请说出来,我将进行相应的编辑。

用于XML阅读

    // Load Document
    XDocument _doc = XDocument.Load("C:\\t\\My File2.txt");
    // Get all Entity elements and put them into a list.
    List<XElement> employees = _doc.XPathSelectElements("ROOT/Entity").ToList();

    // Next you can loop thru the list to check the Entity's elements
    foreach (var employee in employees)
    {
        // to get the armor element: 
        string armor = employee.Element("Armor").Value;
        // to get the rarity element:
        string rarity = employee.Element("Rarity").Value;
        // to get the Stat element:
        string stat = employee.Element("Stats").Element("Stat").Value;
        // to get the Slot element:
        string slot = employee.Element("Slots").Element("Slot").Value;
    }

    // To get one element specific by attribute use this (I check on attribute ID):
      XElement emp = _doc.XPathSelectElements("ROOT/Entity").FirstOrDefault(c => c.Attribute("ID").Value == "0");
    // Next you can extract information from this element just like in the foreach loop.
//加载文档
XDocument\u doc=XDocument.Load(“C:\\t\\My File2.txt”);
//获取所有实体元素并将它们放入列表中。
List employees=_doc.XPathSelectElements(“根/实体”).ToList();
//接下来,您可以在列表中循环检查实体的元素
foreach(员工中的var员工)
{
//要获得装甲元素:
字符串armor=employee.Element(“armor”).Value;
//要获取稀有元素,请执行以下操作:
字符串稀有性=employee.Element(“稀有性”).Value;
//要获取Stat元素,请执行以下操作:
字符串stat=employee.Element(“Stats”).Element(“stat”).Value;
//要获取插槽元素,请执行以下操作:
字符串slot=employee.Element(“slot”).Element(“slot”).Value;
}
//要按属性获取一个特定元素,请使用以下命令(我检查属性ID):
XElement emp=_doc.XPathSelectElements(“根/实体”).FirstOrDefault(c=>c.Attribute(“ID”).Value==“0”);
//接下来,您可以从这个元素中提取信息,就像在foreach循环中一样。

为什么你认为XML是“先进的”?“MichaelKay,我打算把QueTes放进HAHA。我真的不认为它是先进的,但在相对规模上对我来说它是先进的。
    // Load Document
    XDocument _doc = XDocument.Load("C:\\t\\My File2.txt");
    // Get all Entity elements and put them into a list.
    List<XElement> employees = _doc.XPathSelectElements("ROOT/Entity").ToList();

    // Next you can loop thru the list to check the Entity's elements
    foreach (var employee in employees)
    {
        // to get the armor element: 
        string armor = employee.Element("Armor").Value;
        // to get the rarity element:
        string rarity = employee.Element("Rarity").Value;
        // to get the Stat element:
        string stat = employee.Element("Stats").Element("Stat").Value;
        // to get the Slot element:
        string slot = employee.Element("Slots").Element("Slot").Value;
    }

    // To get one element specific by attribute use this (I check on attribute ID):
      XElement emp = _doc.XPathSelectElements("ROOT/Entity").FirstOrDefault(c => c.Attribute("ID").Value == "0");
    // Next you can extract information from this element just like in the foreach loop.