5 回答

TA貢獻1836條經驗 獲得超3個贊
這就是我為實現這一目標所做的工作。
var match = Regex.Match(xml.Document.ToString(), @"KeyValueOfstringOutcome.*>");
if (match.Success)
{
var replacedText = Regex.Replace(xml.Document.ToString(), @"KeyValueOfstringOutcome.*>", "KeyValueOfstringOutcome>");
}

TA貢獻1784條經驗 獲得超9個贊
如果我對你的理解是正確的,你想擺脫Test1,Test2等等。我建議使用這種Replace方法。
string replacedXML = Regex.Replace(xml.Document.ToString(),
@"KeyValueOfstringOutcomeTest\d+",
"KeyValueOfstringOutcome");
\d+將匹配關鍵字后面出現的 1 次或多次數字

TA貢獻1982條經驗 獲得超2個贊
如果有幫助,試試這個。
string result = Regex.Replace(xml.Document.ToString(), "[a-zA-Z0-9]*KeyValueOfstringOutcome[a-zA-Z0-9]*","KeyValueOfstringOutcome");

TA貢獻1834條經驗 獲得超8個贊
我不會REGEX用來匹配和更改節點的名稱。如果你曾經更新過你的文件或命名,它就會崩潰或改變一些其他包含流行語的節點。
childnode所以我會在你想要更改的 lvl/hierarchy 中創建一個循環,然后抓住它并重命名它。
foreach (XmlNode node in doc.ChildNodes)
{
// [2] should be the the deepth of childenotes named `KeyValueOfstringOutcomeTestX`
node.ChildNode[2].Name = "YOUR NEW NAME TO THIS NODE";
}
doc.Save(); // Change renamed nodes to the document.

TA貢獻1818條經驗 獲得超3個贊
使用 xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication107
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> keyValueOfstringOutcomeTests = doc.Descendants().Where(x => x.Name.LocalName.StartsWith("KeyValueOfstringOutcomeTest")).ToList();
foreach (XElement keyValueOfstringOutcomeTest in keyValueOfstringOutcomeTests)
{
keyValueOfstringOutcomeTest.ReplaceWith(new XElement("KeyValueOfstringOutcomeTest", keyValueOfstringOutcomeTest.Elements()));
}
}
}
}
- 5 回答
- 0 關注
- 134 瀏覽
添加回答
舉報