开发者

Replace xml tag with regex

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

<t开发者_JAVA技巧ag1></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


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.


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜