猿问

在字符串中查找单词并替换它

我有一个字符串 [XML],我想找到包含一个词的标签并将该标签替换为另一个名称。


我试过使用这个,但没有成功:


var regex = new Regex(@"\bKeyValueOfstringOutcome\b", RegexOptions.IgnoreCase);

string result = regex.Replace(xml.Document.ToString(), "");

这是我的 XML:


<Response>

  <Outcome>

    <KeyValueOfstringOutcomeTest1>

      <Key>Icon</Key>

      <Value>

        <DataType>System.String</DataType>

        <Field>Icon</Field>

        <Value>O</Value>

      </Value>

    </KeyValueOfstringOutcomeTest1>

    <KeyValueOfstringOutcomeTest2>

      <Key>IconDescription</Key>

      <Value>

        <DataType>System.String</DataType>

        <Field>IconDescription</Field>

        <Value>Old</Value>

      </Value>

    </KeyValueOfstringOutcomeTest2>

    <KeyValueOfstringOutcomeTest3>

      <Key>IconLongDescription</Key>

      <Value>

        <DataType>System.String</DataType>

        <Field>IconLongDescription</Field>

        <Value>Older</Value>

      </Value>

    </KeyValueOfstringOutcomeTest3>

  </Outcome>

</Response>

我需要找到包含 KeyValueOfstringOutcome 的节点,并将 KeyValueOfstringOutcomeTest1、KeyValueOfstringOutcomeTest2、KeyValueOfstringOutcomeTest3 替换为 KeyValueOfstringOutcome。


慕无忌1623718
浏览 147回答 5
5回答

米脂

这就是我为实现这一目标所做的工作。var match = Regex.Match(xml.Document.ToString(), @"KeyValueOfstringOutcome.*>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (match.Success)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var replacedText = Regex.Replace(xml.Document.ToString(), @"KeyValueOfstringOutcome.*>", "KeyValueOfstringOutcome>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

千万里不及你

如果我对你的理解是正确的,你想摆脱Test1,Test2等等。我建议使用这种Replace方法。string replacedXML = Regex.Replace(xml.Document.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@"KeyValueOfstringOutcomeTest\d+",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "KeyValueOfstringOutcome");\d+将匹配关键字后面出现的 1 次或多次数字

临摹微笑

如果有帮助,试试这个。string&nbsp;result&nbsp;=&nbsp;Regex.Replace(xml.Document.ToString(),&nbsp;"[a-zA-Z0-9]*KeyValueOfstringOutcome[a-zA-Z0-9]*","KeyValueOfstringOutcome");

MMMHUHU

我不会REGEX用来匹配和更改节点的名称。如果你曾经更新过你的文件或命名,它就会崩溃或改变一些其他包含流行语的节点。childnode所以我会在你想要更改的 lvl/hierarchy 中创建一个循环,然后抓住它并重命名它。foreach (XmlNode node in doc.ChildNodes){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // [2] should be the the deepth of childenotes named `KeyValueOfstringOutcomeTestX`&nbsp; &nbsp; node.ChildNode[2].Name = "YOUR NEW NAME TO THIS NODE";&nbsp;&nbsp;}doc.Save();&nbsp; // Change renamed nodes to the document.

泛舟湖上清波郎朗

使用 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{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<XElement> keyValueOfstringOutcomeTests = doc.Descendants().Where(x => x.Name.LocalName.StartsWith("KeyValueOfstringOutcomeTest")).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (XElement keyValueOfstringOutcomeTest in keyValueOfstringOutcomeTests)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyValueOfstringOutcomeTest.ReplaceWith(new XElement("KeyValueOfstringOutcomeTest", keyValueOfstringOutcomeTest.Elements()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答