• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • 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 OlivaresCommented:
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 OlivaresCommented:
How many different cases?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 OlivaresCommented:
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 OlivaresCommented:
Well, indeed a single & is not good, it should be &amp;
0
 
Jaime OlivaresCommented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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