简体   繁体   English

查找和替换XML文件

[英]Find and Replace XML file

I have a XML sitemap file which looks like this 我有一个看起来像这样的XML网站地图文件

<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/web/201/hello_world1.html</loc>
</url>
<url>
<loc>http://www.example.com/web/202/hello_world2.html</loc>
</url>
<url>
<loc>http://www.example.com/web/203/hello_world3.html</loc>
</url>
</urlset>

and I want to change it to like 我想将其更改为喜欢

<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/web2/1/hello_world1.html</loc>
</url>
<url>
<loc>http://www.example.com/web2/1/hello_world2.html</loc>
</url>
<url>
<loc>http://www.example.com/web2/1/hello_world3.html</loc>
</url>
</urlset>

I want to change the different IDs 201, 202, 203 with just 1 ID, normal find and replace don't do that, Any idea how to do that in C# or suggest me a Regular Expression to do it in efficient way. 我想只用1个ID来更改不同的ID 201、202、203,正常查找和替换不这样做,任何想法如何在C#中做到这一点或建议我使用正则表达式以高效的方式完成。 Thanks 谢谢

将XML视为字符串,并用正则表达式替换:

new Regex("web/[0-9]+/").Replace(youxmlstr, "web2/1/");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM