亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在字符串中查找單詞并替換它

在字符串中查找單詞并替換它

C#
慕無忌1623718 2022-11-21 20:38:15
我有一個字符串 [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。
查看完整描述

5 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

這就是我為實現這一目標所做的工作。


var match = Regex.Match(xml.Document.ToString(), @"KeyValueOfstringOutcome.*>");

                if (match.Success)

                {

                    var replacedText = Regex.Replace(xml.Document.ToString(), @"KeyValueOfstringOutcome.*>", "KeyValueOfstringOutcome>");

                }


查看完整回答
反對 回復 2022-11-21
?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

如果我對你的理解是正確的,你想擺脫Test1,Test2等等。我建議使用這種Replace方法。


string replacedXML = Regex.Replace(xml.Document.ToString(),

                                   @"KeyValueOfstringOutcomeTest\d+",

                                    "KeyValueOfstringOutcome");

\d+將匹配關鍵字后面出現的 1 次或多次數字


查看完整回答
反對 回復 2022-11-21
?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

如果有幫助,試試這個。

string result = Regex.Replace(xml.Document.ToString(), "[a-zA-Z0-9]*KeyValueOfstringOutcome[a-zA-Z0-9]*","KeyValueOfstringOutcome");


查看完整回答
反對 回復 2022-11-21
?
MMMHUHU

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.


查看完整回答
反對 回復 2022-11-21
?
泛舟湖上清波郎朗

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()));

            }

        }

    }

}


查看完整回答
反對 回復 2022-11-21
  • 5 回答
  • 0 關注
  • 134 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號