首页 > 解决方案 > 通过 c# forrm app 将 xml 文件中的值相乘

问题描述

我想用 C# 为游戏制作一个 XML 编辑器。我已经准备好 ac# forms 应用程序,但我需要弄清楚如何在选定的 xml 文件中乘以值。

XML 如下所示:

<missions>
<mission type="harvest" reward="2862" status="0" success="false">
    <field id="27" sprayFactor="0.000000" spraySet="false" plowFactor="0.000000" state="2" vehicleGroup="3" vehicleUseCost="520.447510" growthState="7" limeFactor="1.000000" weedFactor="1.000000" fruitTypeName="SOYBEAN"/>
    <harvest sellPoint="12" expectedLiters="10539.061523" depositedLiters="0.000000"/>
</mission>
<mission type="harvest" reward="2699" status="0" success="false">
    <field id="4" sprayFactor="0.500000" spraySet="false" plowFactor="0.000000" state="2" vehicleGroup="11" vehicleUseCost="490.897491" growthState="6" limeFactor="1.000000" weedFactor="1.000000" fruitTypeName="COTTON"/>
    <harvest sellPoint="17" expectedLiters="13012.056641" depositedLiters="0.000000"/>
</mission>
<mission type="harvest" reward="8620" status="0" success="false">
    <field id="6" sprayFactor="1.000000" spraySet="false" plowFactor="1.000000" state="2" vehicleGroup="8" vehicleUseCost="1567.417480" growthState="5" limeFactor="1.000000" weedFactor="1.000000" fruitTypeName="SUNFLOWER"/>
    <harvest sellPoint="12" expectedLiters="54337.136719" depositedLiters="0.000000"/>
</mission>
<mission type="transport" reward="307" status="0" success="false" timeLeft="114865506" config="WATER" pickupTrigger="TRANSPORT04" dropoffTrigger="TRANSPORT01" objectFilename="data/objects/pallets/missions/transportPalletBottles.i3d" numObjects="1"/>

我想乘以所有值reward="xxxxx"

我的 c# 代码如下所示:

 public Form1()
    {
        InitializeComponent();
        CenterToScreen();
        string multiply = textBox1.Text;
    }

    XmlDocument missions;
    string path;
    private string lang;

    public void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (radioButton2.Checked == true)
        {
            lang = "en";
        }
    }

    public void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (radioButton1.Checked == true)
        {
            lang = "nl";
        }
    }

    public void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string strfilelocation = openFileDialog1.FileName;
            textBox2.Text = strfilelocation;
            path = strfilelocation;
            missions = new XmlDocument();
            missions.Load(path);
        }
    }

    public void button4_Click(object sender, EventArgs e)
    {
        if (lang == "en")
        {
            MessageBox.Show("This tool is developed to make the wages of the contractjobs higher, thus giving you more money when you complete them. Select the desired percentage wich you want the program to multiply the wages with, and select the missions.xml in your desired savegame. Click apply, and then save. You need to run this program every once in a while, to update the wages, or use it once and copy the file. (disclaimer: when you have raised wages, and you run teh program again, and jobs with raised wages are still in missions.xml, the wages will get even higher then is was.)");
        } else if (lang == "nl")
        {
            MessageBox.Show("This tool is developed to make the wages of the contractjobs higher, thus giving you more money when you complete them. Select the desired percentage wich you want the program to multiply the wages with, and select the missions.xml in your desired savegame. Click apply, and then save. You need to run this program every once in a while, to update the wages, or use it once and copy the file. (disclaimer: when you have raised wages, and you run teh program again, and jobs with raised wages are still in missions.xml, the wages will get even higher then is was.)");
        }
        }

是否有捷径可寻?如果是的话,有人可以指出我的方向吗?我不是要求所有的代码,只是一个小小的启动,因为我被困在这一点上。

提前致谢!

///////////////////////////////////新错误;

System.Xml.XmlException: '根级别的数据无效。第 1 行,位置 1。

代码:

var text = path;

        var xml = XElement.Parse(text);
        var rewards = xml
                      .Descendants()
                      .Where(d => d.Attribute("reward") != null)
                      .Select(d => d.Attribute("reward"));
        // Do something with rewards. For instance, displaying them in the console
        rewards.ToList().ForEach(r => Console.WriteLine(r.Value));

标签: c#winforms

解决方案


我已经对 XML 进行了一些精简,以省略我们不需要的文本。该代码提取所有奖励。您可以随心所欲地使用它们:

var text = @"
    <missions>
        <mission type='harvest' reward='2862' status='0' success='false'>
        </mission>
        <mission type='harvest' reward='2699' status='0' success='false'>
        </mission>
        <mission type='harvest' reward='8620' status='0' success='false'>
        </mission>
        <mission type='transport' reward='307' status='0' success='false' />
    </missions>";

var xml = XElement.Parse(text);
var rewards = xml
              .Descendants()
              .Where(d => d.Attribute("reward") != null)
              .Select(d => d.Attribute("reward"));
// Do something with rewards. For instance, displaying them in the console
rewards.ToList().ForEach(r => Console.WriteLine(r.Value));

推荐阅读