C#和浏览xml文件 [英] c# and navigating an xml file

查看:91
本文介绍了C#和浏览xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml文件"ScoreTable.xml":

I have the following xml file "ScoreTable.xml":

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ScoreTable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Table>
        <Score>1</Score>
        <Modifier>-10</Modifier>
    </Table>
    <Table>
        <Score>2</Score>
        <Modifier>-9</Modifier>
    </Table>
    <Table>
        <Score>3</Score>
        <Modifier>-8</Modifier>
    </Table>
    <Table>
        <Score>4</Score>
        <Modifier>-7</Modifier>
    </Table>
    <Table>
        <Score>5</Score>
        <Modifier>-6</Modifier>
    </Table>
    <Table>
        <Score>6</Score>
        <Modifier>-5</Modifier>
    </Table>
    <Table>
        <Score>7</Score>
        <Modifier>-4</Modifier>
    </Table>
    <Table>
        <Score>8</Score>
        <Modifier>-3</Modifier>
    </Table>
    <Table>
        <Score>9</Score>
        <Modifier>-2</Modifier>
    </Table>
    <Table>
        <Score>10</Score>
        <Modifier>-1</Modifier>
    </Table>
    <Table>
        <Score>11</Score>
        <Modifier>0</Modifier>
    </Table>
    <Table>
        <Score>12</Score>
        <Modifier>1</Modifier>
    </Table>
    <Table>
        <Score>13</Score>
        <Modifier>2</Modifier>
    </Table>
    <Table>
        <Score>14</Score>
        <Modifier>3</Modifier>
    </Table>
    <Table>
        <Score>15</Score>
        <Modifier>4</Modifier>
    </Table>
    <Table>
        <Score>16</Score>
        <Modifier>5</Modifier>
    </Table>
    <Table>
        <Score>17</Score>
        <Modifier>6</Modifier>
    </Table>
    <Table>
        <Score>18</Score>
        <Modifier>7</Modifier>
    </Table>
    <Table>
        <Score>19</Score>
        <Modifier>8</Modifier>
    </Table>
    <Table>
        <Score>20</Score>
        <Modifier>4</Modifier>
    </Table>



我具有通过随机数生成器生成的1到20之间的值.我想解析xml文件,并将此值与得分"元素中的值进行比较.找到匹配项后,我想返回与等于值"的得分"元素配对的修饰符"元素值.我目前了解如何访问xml文件并可以打印"Score"和"Modifier"值,但是如何解析一个元素并返回另一个元素的值尚不清楚.

我目前拥有的代码是:



I have Value that I''ve generated thru a random number generator to be between 1 and 20. I''d like to parse the xml file and compare this Value to the values in the "Score" elements. When I find a match, I''d like to return the "Modifier" element value that is paired with the "Score" element that equals the Value. I currently understand how to access the xml file and can print the "Score" and "Modifier" values, but it is not apparent how to parse one element and return the value of another element.

The code I currently have is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
// using System.Xml.Linq;
namespace xml_read_and_parse
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmple = "15";
            
            XmlDocument doc = new XmlDocument();
            doc.Load("abilityModifierTable.xml");
            XmlNodeList elemList = doc.GetElementsByTagName("Table");
            for (int i = 0; i < elemList.Count; i++)
            {
                Console.WriteLine(elemList[i].InnerXml);
            }
            
            Console.ReadLine();
        }
        
    }
}



任何建议将不胜感激.

敬重,
SIGpimp



Any suggestions would be greatly appreciated.

Much respect,
SIGpimp

推荐答案

每次获取 Score 标记时都需要检查,并将文本值转换为数字; Int32.TryParse() [
You need to check every time you get a Score tag and convert the text value to a number; Int32.TryParse()[^] would be a good method to use. Then if it matches your value just trap the next Modifier tag and you have the matching value.


好.我做完了但是更多的是蛮力,然后是优雅.

使用上面显示的相同xml文件:

Ok. I''ve done it. But more by brute force then elegance.

Using the same xml file shown above:

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

namespace xml_read_and_parse
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmple = "15";
            string goBack = "";
            
            XmlDocument doc = new XmlDocument();
            doc.Load("abilityModifierTable.xml");
            XmlNodeList elemList = doc.GetElementsByTagName("Table");
            XmlNodeList scoreList = doc.GetElementsByTagName("Score");
            XmlNodeList modList = doc.GetElementsByTagName("Modifier");
            
                        
            for (int i = 0; i < elemList.Count; i++)
            {                
                
                if (scoreList[i].InnerText.Equals(xmple))
                {
                    goBack = modList[i].InnerText;
                    Console.WriteLine(goBack);
                    break;
                }
                                
            }
            
            Console.ReadLine();
        }



但是,我想尝试理解为什么我不能轻易地解析初始XmlNodeList"elemList".我确定我缺少什么... XML应该是非常灵活的,并且是自切成薄片的苹果派以来最伟大的事情",但对我来说似乎并不直观.

预先感谢,
SIGpimp



However, I''d like to try and understand why I can''t easily parse the initial XmlNodeList "elemList." I''m sure I''m missing something...XML is supposed to be incredibly flexible and the "greatest thing since sliced apple pie," but it does not seem intuitive to me.

Thanks in advance,
SIGpimp


这篇关于C#和浏览xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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