所以,我正在写一个程序,我有一个带有攻击移动的xml文件,我希望用我的值替换损坏值,我已经有了我的值,并附有一个名字,例子是
“滑车| 2D8”
现在,在我的xml文件中,它如下:
<Move>
<Name>Steamroller</Name>
<Type>Bug</Type>
<Frequency>EOT</Frequency>
<Accuracy>2</Accuracy>
<Range>Melee</Range>
<Damage>1d10+10</Damage>
<Class>Physical</Class>
<Targets>1 Target, Pass</Targets>
<Effect>Steamroller Flinches the target on 15-20 during Accuracy Check. If the target is Small, Steamroller deals an additional 1d10.</Effect>
<ContestType>Tough</ContestType>
<Appeal>3d4</Appeal>
<ContestEffect>No Effect</ContestEffect>
<Special />
</Move>
<Move>
<Name>String Shot</Name>
<Type>Bug</Type>
<Frequency>At-Will</Frequency>
<Accuracy>3</Accuracy>
<Range>6</Range>
<Damage />
<Class>None</Class>
<Targets>1 Target, Column</Targets>
<Effect>String Shot creates a Column 2-meters wide. All Legal Targets within the Column lower their Speed 1 Combat Stage. If a target is hit by String Shot 5 times within 5 rounds of combat, they are Trapped. If a target's Speed Combat Stage has already been lowered 6 times, String Shot Traps them.</Effect>
<ContestType>Smart</ContestType>
<Appeal>2d4</Appeal>
<ContestEffect>Excitement</ContestEffect>
<Special>Threaded</Special>
</Move>
所以基本上我想要替换<Damage>
和</Damage>
之间的文本,但我不知道如何解决这个问题,目前我所拥有的是两个数组,其中一个包含我的移动他们的伤害,另一个是Xml文件移动拆分(使用string.Split("</Move>")
)然后我循环通过这两个:
for (int index = 0; index < xmlMoves.Length; index++)
{
foreach (var move in myMovesArray)
{
//myMoves.Split('|')[0] is the name of the move,
//i'm comparing that the move i am at in my Array is the same as the one in the Array of Xml Moves
if (xmlMoves[index].Contains(myMoves.Split('|')[0]))
{
//Replace stuff here
}
}
}
如果有人能提供帮助那就太棒了,谢谢!
答案 0 :(得分:2)
你走了!
如评论中所述,您希望将xmlString解析为实际的XML对象,以便您可以轻松访问这些元素。这使用XDocument,因此您需要将using System.Xml.Linq;
添加到文件的顶部。
private void XMltest()
{
string filePath = @"path to your xml";
string[] myMovesArray = new string[2] {"Steamroller|2d8", "String Shot|4d10"};
// Add a root to your xml since it wasn't shown in your example
// NOTE: Remove these next two lines if your xml string does indeed have a root.
string xmlFragments = File.ReadAllText(filePath);
string rootedXML = "<root>" + xmlFragments + "</root>";
// load the xml
XDocument xmlMoves = XDocument.Parse(rootedXML);
// Loop through array and update associated element in xml with the new damage values
foreach (var move in myMovesArray)
{
string moveName = move.Split('|')[0];
string moveDamage = move.Split('|')[1];
// Loop through xml and find the correct element by name
foreach (XElement xmlMove in xmlMoves.Descendants("Move"))
{
if (xmlMove.Element("Name").Value == moveName)
{
xmlMove.Element("Damage").Value = moveDamage;
}
}
}
// Save back to xml file.
xmlMoves.Save(filePath);
}
答案 1 :(得分:1)
根据评论,不要使用文本操作来执行此操作。使用LINQ to XML非常简单:
var doc = XDocument.Parse(xml);
foreach (var move in doc.Descendants("Move"))
{
if ((string) move.Element("Name") == "Steamroller")
{
var damage = move.Element("Damage");
if (damage != null)
{
damage.Value = "2d8";
}
}
}
有关正常工作的演示,请参阅this fiddle。
答案 2 :(得分:0)
您可以使用向后和向前的前瞻进行正则表达式替换:
(?<=(STR_BEFORE)))
(?=(STR_AFTER))
所以,例如(?<=(abc))def(?=(ghi))
只有在前面有abc并且后面是ghi