使用数学编辑 XML 文件的值
Edit the values of an XML file using math
我有一个 XML 文件,内容如下:
<CHandlingDataMgr>
<HandlingData>
<Item type="CHandlingData">
<handlingName>AIRTUG</handlingName>
<fMass value="1400.000000" />
<fSteeringLock value="30.000000" />
<SubHandlingData>
<Item type="NULL" />
<Item type="NULL" />
<Item type="NULL" />
</SubHandlingData>
</Item>
<Item type="CHandlingData">
<handlingName>ADDER</handlingName>
<fMass value="1800.000000" />
<fSteeringLock value="42.000000" />
<SubHandlingData>
<Item type="CCarHandlingData">
<fBackEndPopUpCarImpulseMult value="0.075000" />
<fBackEndPopUpBuildingImpulseMult value="0.030000" />
<fBackEndPopUpMaxDeltaSpeed value="0.250000" />
</Item>
<Item type="NULL" />
<Item type="NULL" />
</SubHandlingData>
</Item>
</HandlingData>
</<CHandlingDataMgr>
如果我想将所有 "fMass" 标签的值减少 25% 并将所有 "fSteeringlock" 值的值减少 50%,我该怎么做?我应该使用什么工具?
试试 C# XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication37
{
class Program
{
const string FILENAME = @"\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> Items = doc.Descendants("Item").ToList();
XAttribute value = null;
foreach (XElement item in Items)
{
XElement fMass = item.Element("fMass");
if (fMass != null)
{
value = item.Element("fMass").Attribute("value");
fMass.Value = (0.75 * ((double)value)).ToString();
}
XElement fSteeringLock = item.Element("fSteeringLock");
if (fSteeringLock != null)
{
value = item.Element("fSteeringLock").Attribute("value");
fSteeringLock.Value = (0.5 * ((double)value)).ToString();
}
}
doc.Save(FILENAME);
}
}
}
我有一个 XML 文件,内容如下:
<CHandlingDataMgr>
<HandlingData>
<Item type="CHandlingData">
<handlingName>AIRTUG</handlingName>
<fMass value="1400.000000" />
<fSteeringLock value="30.000000" />
<SubHandlingData>
<Item type="NULL" />
<Item type="NULL" />
<Item type="NULL" />
</SubHandlingData>
</Item>
<Item type="CHandlingData">
<handlingName>ADDER</handlingName>
<fMass value="1800.000000" />
<fSteeringLock value="42.000000" />
<SubHandlingData>
<Item type="CCarHandlingData">
<fBackEndPopUpCarImpulseMult value="0.075000" />
<fBackEndPopUpBuildingImpulseMult value="0.030000" />
<fBackEndPopUpMaxDeltaSpeed value="0.250000" />
</Item>
<Item type="NULL" />
<Item type="NULL" />
</SubHandlingData>
</Item>
</HandlingData>
</<CHandlingDataMgr>
如果我想将所有 "fMass" 标签的值减少 25% 并将所有 "fSteeringlock" 值的值减少 50%,我该怎么做?我应该使用什么工具?
试试 C# XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication37
{
class Program
{
const string FILENAME = @"\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> Items = doc.Descendants("Item").ToList();
XAttribute value = null;
foreach (XElement item in Items)
{
XElement fMass = item.Element("fMass");
if (fMass != null)
{
value = item.Element("fMass").Attribute("value");
fMass.Value = (0.75 * ((double)value)).ToString();
}
XElement fSteeringLock = item.Element("fSteeringLock");
if (fSteeringLock != null)
{
value = item.Element("fSteeringLock").Attribute("value");
fSteeringLock.Value = (0.5 * ((double)value)).ToString();
}
}
doc.Save(FILENAME);
}
}
}