2

I am trying to match the Location tag in an XML and replace "\" with "\\" in the xml content only for the Location tag ,can anyone provide guidance on how to do that?

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();
        }
    }
}
cup
  • 7,589
  • 4
  • 19
  • 42
dot net
  • 253
  • 5
  • 10
  • 3
    Please clearly state the requirement. _and replace "\" with "\" ..._ They're both the same. – Sach Aug 29 '17 at 19:15
  • I think the title of the issue has it, in that he wants a single slash replaced with double-slash. Our text editors here may be processing that out strangely. As for the problem, this feels like a "use LINQ to traverse the XML and change what you want in-place" kind of problem. – Kevin Anderson Aug 29 '17 at 19:17
  • It is an anomaly in the SO editor - \\ appears a \. You need to type \\\ to get \\ – cup Aug 29 '17 at 19:18
  • 1
    I sugget find specific tag using, for example `XPath`, and replace "\" directly for him. – George Alexandria Aug 29 '17 at 19:18
  • @GeorgeAlexandria - Can you provide a specific example on how to do that? – dot net Aug 29 '17 at 19:21

2 Answers2

4

You don't need regex. Use a xml parser like Linq2Xml

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

string newXml = xDoc.ToString();

PS: A good SO post to read. RegEx match open tags except XHTML self-contained tags

L.B
  • 114,136
  • 19
  • 178
  • 224
0

You can use this code:

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;
}

If you need your xmlcontent be updated then you can write:

xmlcontent = xmlDoc.OuterXml;
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37