[英]XML File changing contents
首先,答案可能非常简单,但是我尝试了许多看起来正常的解决方案,无论我如何尝试都没有使它们起作用。
我得到了这个XML文件:
<?xml version="1.0" encoding="utf-8"?>
<Config>
<PopulationSize>100</PopulationSize>
<SpecieCount>20</SpecieCount>
<Activation>
<Scheme>FixedIters</Scheme>
<Iters>1</Iters>
</Activation>
<ComplexityRegulationStrategy>Absolute</ComplexityRegulationStrategy>
<ComplexityThreshold>500</ComplexityThreshold>
<Description>
Helikopter game
</Description>
<Timesteps>50000</Timesteps> //Length of the world (x-axis)
<Worldheight>200</Worldheight> //Height of the world (y-axis)
<SensorInputs>10</SensorInputs> //Length of one side of the rectangle that is used as the input. So 15 here means 15*15 = 225 inputs
<Speler>computer</Speler>
</Config>
我想编辑
<Speler>computer</Speler>
像
<Speler>mens</Speler>
目前,我想要一些类似的东西:
XmlDocument doc = new XmlDocument();
doc.Load("Helikopter.config.xml");
//Change the contents of "Speler" to "Mens"
doc.Save("Helikopter.config.xml");
但是我似乎无法使它正常工作,无论我尝试放置在这里什么,我已经在这里尝试了很多选择。
求助,谢谢
这样的事情怎么样:
XmlDocument doc = new XmlDocument();
doc.Load("Helikopter.config.xml");
XmlNode node;
node = doc.DocumentElement;
// Iterate through all nodes
foreach(XmlNode node1 in doc.ChildNodes)
{
// if the node is speler
if(node1.Name == "speler")
{
// Change inner text to mens
node1.InnerText = "mens";
}
}
doc.Save("Helikopter.config.xml");
static void Main(string[] args)
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Config>
<PopulationSize>100</PopulationSize>
<SpecieCount>20</SpecieCount>
<Activation>
<Scheme>FixedIters</Scheme>
<Iters>1</Iters>
</Activation>
<ComplexityRegulationStrategy>Absolute</ComplexityRegulationStrategy>
<ComplexityThreshold>500</ComplexityThreshold>
<Description>
Helikopter game
</Description>
<Timesteps>50000</Timesteps> //Length of the world (x-axis)
<Worldheight>200</Worldheight> //Height of the world (y-axis)
<SensorInputs>10</SensorInputs> //Length of one side of the rectangle that is used as the input. So 15 here means 15*15 = 225 inputs
<Speler>computer</Speler>
</Config>";
XDocument doc = XDocument.Parse(xml);
List<XElement> elements = doc.Descendants("Config").ToList();
foreach (XElement elem in elements)
{
elem.Element("Speler").Value = "mens";
Console.WriteLine(elem.Element("Speler").Value);
}
Console.ReadKey();
}
这里有一个示例,您可以如何做。 您需要参考使用System.Xml.Linq;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.