0

I have huge amount XML files and I need to remove attributes with specific content.

  <Image Width="214.57"
         Height="165"
         HorizontalAlignment="Left"
         VerticalAlignment="Top"
         Source="{DynamicResourceExtension ResourceKey=images_4bd6348715-testImage.jpg}"
         Canvas.Left="8"
         Canvas.Top="24" />

First case:

Operator can enter images_4bd6348715-testImage.jpg and attribute Source should be removed.

Attribute can have any name.

Second case:

Text can be inside node. Node can have any name:

   <Image Width="214.57"
             Height="165"
             HorizontalAlignment="Left"
             VerticalAlignment="Top"
             Canvas.Left="8"
             Canvas.Top="24" >
    <Image.Source2>
        <DynamicResource>
            <DynamicResource.ResourceKey>
                    images_4bd6348715-testImage.jpg
            </DynamicResource.ResourceKey>
        <DynamicResource>
    </Image.Source2>
</Image>

In both cases i only know that inside attribe or node i will have DynamicResource or DynamicResourceExtension

How can i do this with Regex query?

My attemts: First case:

\w+[.]?\w+\s*[=]\s*["]\s*[{]\s*[_.\s{=}a-zA-Z0-9]*images_4bd6348715-testImage.jpg[_.\s{=}a-zA-Z0-9]*\s*}\s*["]
Ievgen
  • 4,261
  • 7
  • 75
  • 124
  • 3
    Why do this with Regex? Why not just open the files with XDocument and remove the attribute? – crthompson Sep 29 '13 at 22:14
  • 1
    Parse the `Xaml` with `XamlReader` and remove all the `Image` `Sources` with that value – sa_ddam213 Sep 29 '13 at 22:17
  • Files can contains invalid XML, XAML. – Ievgen Sep 29 '13 at 22:19
  • 1
    Check top related post - http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg?rq=1 ... If you want to parse/search XML with RegEx you are should endure pain alone :) – Alexei Levenkov Sep 29 '13 at 22:45
  • @AlexeiLevenkov for 'attribute' case its not so hard. And as i get it for a second case i have to try something else :( – Ievgen Sep 29 '13 at 22:50
  • @Evgeny, XAML is well-formed XML. – Kirill Polishchuk Sep 29 '13 at 23:20
  • @paqogomez Do you know how to do this with XDocument? How can we search thru all document for attribute or node value? – Ievgen Sep 29 '13 at 23:20
  • @KirillPolishchuk I mean that some nodes can be not closed or something like that. So XML can be invalid. – Ievgen Sep 29 '13 at 23:21
  • 2
    If it isn't well-formed, then it isn't XML. – Mads Hansen Sep 30 '13 at 00:10
  • Suggest you add the non-well-formed issue to your question, preferably in the title, as it's a critical characteristic. Further, you should establish some invariant against which RE or any code can be written if your input is so uncertain as to be possibly not well-formed, let alone valid, XML. – kjhughes Sep 30 '13 at 01:44
  • @kjhughes Disscussed this with customer. All not well formed XML will be stored in separate folder and operator will manually change files. So Actually XDocument can be used. – Ievgen Sep 30 '13 at 08:30

0 Answers0