• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Insert using Regex

Given the following string

"<Collections>
<Books>
<book>The Lost Symbol</book>
<book>Dark Tower: Wizard & Glass, The</book>
</Books>
<Cards>
<card>
</card>
</Cards>
</Collections>"

I would like to insert CDATA into each node to get the following result

<Collections>
<Books>
<book>![CDATA[The Lost Symbol]]</book>
<book>![CDATA[Dark Tower: Wizard & Glass, The]]</book>
</Books>
<Cards>
<card>
![CDATA[]]
</card>
</Cards>
</Collections>

Can i do this using regex? Would it be possible to just do nodes with special characters?

If not by regex how else can i achieve this efficiently

language i am using is c#

Thanks

Allan
0
acadenilla
Asked:
acadenilla
  • 5
  • 4
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
sure you can. Assuming your xml is already in a string (input):

string strRegex = @"(<book>)(.*?)</book>";
string strReplace = @"<book>![CDATA[$2]]</book>";
Regex myRegex = new Regex(strRegex, RegexOptions.None);

string input = @"<Collections>\n<Books>\n<book>The Lost Symbol</book>\n<book>Dark Tower: Wizard & Glass, The</book>\n</Books>\n<Cards>\n<card>\n</card>\n</Cards>\n</Collections>";

string output = myRegex.Replace(input, strReplace);

Open in new window

0
 
acadenillaAuthor Commented:
thanks jaime that worked.

how about if there was more nodes


"<Collections>
<Books>
<book>The Lost Symbol</book>
<book>Dark Tower: Wizard & Glass, The</book>
</Books>
<Cards>
<card>Mickey Mantle</card>
</Cards>
</Collections>"

<Collections>
<Books>
<book>![CDATA[The Lost Symbol]]</book>
<book>![CDATA[Dark Tower: Wizard & Glass, The]]</book>
</Books>
<Cards>
<card>![CDATA[Mickey Mantle]]
</card>
</Cards>
</Collections>

would i have to create several different regex to achieve this or is there some sort of wild card

maybe "(<\w>(.*?)</w> "?
0
 
Jaime OlivaresSoftware ArchitectCommented:
How many different cases?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
acadenillaAuthor Commented:
Jaime,

There are a few a little over 20.

I could just create some of sort of array of all the node names and loop through it and applying your solution. Just wanted to know if there was some regex that would do this
0
 
Jaime OlivaresSoftware ArchitectCommented:
Yes, you can do that, because having a regex expression with 20 different cases will be a mess.
But for that case I would recommend to load your xml into a XmlDocument class, traverse it and change all the desired tags, but the first option is still valid.
0
 
acadenillaAuthor Commented:
And there were my issue lies. If i try to load the xml string into an XDocument when the xml string has "&" it throws an error


string input = "<Collections><Books><book>The Lost Symbol</book><book>Dark Tower: Wizard & Glass, The</book></Books><Cards><card>Mickey Mant</card></Cards></Collections>";

XDocument doc = XDocument.Parse(input);

Open in new window


hmm maybe it is cause i am using XDocument instead of XmlDocument.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Well, indeed a single & is not good, it should be &amp;
0
 
Jaime OlivaresSoftware ArchitectCommented:
0
 
acadenillaAuthor Commented:
Thanks Jaime, i decided to create an array of all the nodes that i need to do the insert and loop through it and apply the code you have provided
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now