Search code examples
c#xmlregexc#-4.0marc

Need some help manipulating a string in C#?


I have a very large string that I am trying to convert from an old standard to marc21 xml standard.

The following line of code:

temp1 = inputString.Replace("<marc:controlfield tag=\"LDR\">", "<marc:leader>");

Produces the following:

<marc:leader>^^^^^nam^a22^^^^^3a^4500</marc:controlfield>

The problem is quite evident.

I perform a blanket conversion on a particular term and replace it with 'marc:controlfield'. Towards the end of my conversion process, I start to handle the leader element. Which is where I am right now. The xml savvy of out there know that:

</marc:controlfield>

needs to be:

</marc:leader>

Once this is done, my string can be tested for well formedness and validity and etc. I am struggling on how to grab the closing brackets for a leader element and replace it with xml as shown above.

Originally the Leader element looks like:

<fixfield id="LDR">^^^^^nam^a22^^^^^3a^4500</fixfield>

Any help is greatly appreciated.


Solution

  • use Regex:

    strResult = Regex.Replace(inputString,
                              "<marc:controlfield tag=\"LDR\">([^<]*)</marc:controlfield>",
                              "<marc:leader>$1</marc:leader>");  
    

    explain:

    [^<]*  
    
    means any character except: '<' 
    (0 or more times, matching the most amount possible)