winforms 通过c#表单应用程序在xml文件中乘法值

d6kp6zgx  于 2022-11-25  发布在  C#
关注(0)|答案(1)|浏览(238)

我想用C#为一个游戏制作一个XML编辑器。我已经准备好了一个C#窗体应用程序,但我需要弄清楚如何将选定的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 which 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 the program again, and jobs with raised wages are still in missions.xml, the wages will get even higher than they were.)");
        } 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 which 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 the program again, and jobs with raised wages are still in missions.xml, the wages will get even higher than they were.)");
        }
        }

有没有简单的方法可以做到这一点?如果有,有人能给我指出方向吗?我不要求所有的代码,只是一个小的启动,因为我在这一点上卡住了。
提前感谢!
新错误
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));
f2uvfpb9

f2uvfpb91#

我对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));

相关问题