1

How can I replace a certain part in a xml file with a definied string?

<tag1></tag2>
<tag2></tag2>
...etc
<soundcard num=0> 
<name>test123</name>
</soundcard>
<soundcard num=1> 
<name>test123</name>
</soundcard>
<soundcard num=2> 
<name>test123</name>
</soundcard>
<tag5></tag5>

replace all soundcard parts that the result looks like that:

<tag1></tag2>
<tag2></tag2>
...etc
{0}
<tag5></tag5>

I'm using c# .net 3.5 and I thougt of a regex solution

5
  • 4
    No not use regular expressions to parse, modify or otherwise handle XML and other non-regular languages (such as HTML, JSON, YAML, ...). The proper solution here is to use the XML APIs to parse and alter the document tree however you like. Commented Apr 16, 2010 at 19:40
  • @Johannes: You mean Do not. Commented Apr 16, 2010 at 19:41
  • 1
    DO NOT PARSE XML USING Regular Expressions! Commented Apr 16, 2010 at 19:42
  • It's remarkable how many people try to parse XML/HTML with regex... Commented Apr 16, 2010 at 19:45
  • @SLaks: Argh, yes; too late to fix a typo now, though. Commented Apr 16, 2010 at 20:16

3 Answers 3

3

If it has to be a regex, your XML file is well-formed, and you know (say, from the DTD) that <soundcard> tags can't be nested, then you can use

(<soundcard.*?</soundcard>\s*)+

and replace all with {0}.

In C#:

resultString = Regex.Replace(subjectString, @"(<soundcard.*?</soundcard>\s*)+", "{0}", RegexOptions.Singleline);

For a quick-and-dirty fix to a one-off problem, I think that's OK. It's not OK to think of regex as the proper tool to handle XML in general.

Sign up to request clarification or add additional context in comments.

Comments

2

Personally I would use Linq to XML and remove the entities and replace it with a Text Node.

Update Apr 16/2010 4:40PM MST

Here's an example of Linq to XML, I'm a bit rusty but it should at least give you an idea of how this is done.

XElement root = XElement.Load("myxml.xml");

var soundcards = select el from root.Elements() where el.Name == "soundcard" select el;
var prev_node = soundcards.First().PreviousNode;

// Remove Nodes
foreach(XElement card in soundcards)
    card.Remove();

// Build your content here into a variable called newChild

prev_node.AddAfterSelf(newChild);

Comments

1

My suggestion would be to use an XSLT transformation to replace the tags you want to replace with a known tag, say , and then String.Replace('', '{0}');.

I echo what Johannes said, do NOT try to build REs to do this. As your XML gets more complex, you error rate will increase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.