1

I have this XML, and i am trying to delete those: Name="items" Name="shipmentsShipmentIndex"

"Name" is not going to change,

The word that will be change is the word between the quotation marks, in this example: items and shipmentsShipmentIndex

<?xml version="1.0" encoding="UTF-8"?>

 <RootDTO xmlns:json='http://james.newtonking.com/projects/json'>
   <destination>
      <name>XXX</name>
   </destination>
   <orderData>    
      <items json:Array='true'>   
        <shipmentIndex Name="items">0</shipmentIndex>           
      </items>
       <items json:Array='true'>      
        <shipmentIndex Name="items">0</shipmentIndex>           
      </items>

      <shipments json:Array='true'>          
        <shipmentIndex Name="shipmentsShipmentIndex">0</shipmentIndex>                                                          
      </shipments>
   </orderData>
  </RootDTO>

How can i find all these occurrences and remove them(or replace them with nothing)? I tried a bunch of variations like: Name{1}[a-zA-Z]+"\b

Eran Meir
  • 923
  • 3
  • 17
  • 32

3 Answers3

2

Don't parse XML with regular expressions. Use an XML aware tool.

For example, in xsh, you can achieve this with

open file.xml ;
rm //@Name ;
save :b ;
Community
  • 1
  • 1
choroba
  • 231,213
  • 25
  • 204
  • 289
2

You wrote this is ASP.NET. You can use Linq2Xml to perform this operation very quickly (LinqPad example):

void Main()
{
    var test = XDocument.Parse("<root><node name=\"name value\">Text</node></root>");
    foreach(var attr in test.Descendants().SelectMany(el=>el.Attributes("name")))
                attr.Remove();

    test.Dump();
}

So from this:

<root>
  <node name="name value">Text</node>
</root>

You get this:

<root>
  <node>Text</node>
</root>

The snippet will remove ALL name attributes from every single element in the XML aside of the root one. If you want to do something else (like just empty the attribute, or include the root element), let me know.

Gerino
  • 1,943
  • 1
  • 16
  • 21
1

Try this RegEx:

Name=\"(.*?)\"

enter image description here

Harpreet Singh
  • 2,651
  • 21
  • 31