簡體   English   中英

如何只為XML中的特定標記替換“ \\”和“ \\\\”?

[英]How to replace “\” and “\\” for only a specific tag in XML?

我正在嘗試匹配XML中的Location標記,並僅將Location標記的xml內容中的“ \\”替換為“ \\\\”,有人可以提供有關該操作方法的指南嗎?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Matchlocationreplacebackslash
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = "<Location>(.*?)</Location>";
            string xmlcontent = @"<SoftwareProductBuild>
                        <BuildSource>QCA_DEV_POSTCOMMIT</BuildSource>
                        <BuiltBy>wbibot</BuiltBy>
                        <CreatedBy>wbibot</CreatedBy>
                        <Name>BTFM.CHE.2.1.2-00091-QCACHROM-1_NO_VARIANT</Name>
                        <Status>Approved</Status>
                        <BuiltOn>2017-08-28T13:00:04.345Z</BuiltOn>
                        <Tag>BTFM.CHE.2.1.2_BTFM.CHE.2.1.2-00091-QCACHROM-1_2017-08-28T13:00:04.345Z</Tag>
                        <SoftwareImageBuilds>
                            <SoftwareImageBuild>
                                <Type>LA</Type>
                                <Name>BTFM.CHE.2.1.2-00091-QCACHROM-1_NO_VARIANT</Name>
                                <Location>\\snowcone\builds676\INTEGRATION\BTFM.CHE.2.1.2-00091-QCACHROM-1</Location>
                                <Variant>NO_VARIANT</Variant>
                                <LoadType>Direct</LoadType>
                                <Target>NO_VARIANT</Target>
                                <SoftwareImages>
                                    <SoftwareImage>
                                        <Name>BTFM.CHE.2.1.2</Name>
                                        <SoftwareProducts>
                                            <SoftwareProduct>
                                                <Name>MSM8998.LA.1.9</Name>
                                                <BaseMeta>CI_MSM8998.LA.1.9-16991-INT-2</BaseMeta>
                                            </SoftwareProduct>
                                        </SoftwareProducts>
                                    </SoftwareImage>
                                </SoftwareImages>
                            </SoftwareImageBuild>
                        </SoftwareImageBuilds>
                    </SoftwareProductBuild>";
            Match match = Regex.Match(xmlcontent, pattern); //Match location
            //Replace "\" with  "\\" in the xml content with the match
            Console.ReadLine();
        }
    }
}

您不需要正則表達式。 使用XML解析器,如Linq2Xml

var xDoc = XDocument.Parse(xmlcontent);
foreach(var loc in xDoc.Descendants("Location"))
{
    loc.Value = loc.Value.Replace(@"\", @"\\");
}

string newXml = xDoc.ToString();

PS:一個不錯的SO閱讀文章。 RegEx匹配XHTML自包含標簽以外的打開標簽

您可以使用以下代碼:

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlcontent);
var locations = xmlDoc.GetElementsByTagName("Location");
foreach (XmlNode location in locations)
{
    var newLocation = location.InnerText.Replace("\\", "\\\\");
    location.InnerText = newLocation;
}

如果您需要更新xmlcontent則可以編寫:

xmlcontent = xmlDoc.OuterXml;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM