C#如何修改XML节点名称

Mr__zhang 2012-11-02 06:37:12
我有一个xml文件,简化如下:
<material>
<id> 001 </id>
<name> cp1 </name>
</material>


要转换为
<component>
<id> 001 </id>
<caption> cp1 </caption>
</component>

急求!!!




...全文
676 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalmeeme 2012-12-31
  • 打赏
  • 举报
回复
用正则处理一下:
		string filename=Server.MapPath("~/test.xml");
		XmlDocument xmlDoc = new XmlDocument();
		xmlDoc.Load(filename);
		xmlDoc.InnerXml = Regex.Replace(xmlDoc.InnerXml, @"(?s)(?<=</?)material(?=>)", "component");
		xmlDoc.InnerXml = Regex.Replace(xmlDoc.InnerXml, @"(?s)(?<=</?)name(?=>)", "caption");
		xmlDoc.Save(filename);
DENQH 2012-11-04
  • 打赏
  • 举报
回复
 XElement xexl = XElement.Load(path);
                       xexl.Elements().ElementAt(0).Name="component";
                       xexl.Save(path);

Mr__zhang 2012-11-04
  • 打赏
  • 举报
回复
LS未解决 求助,求助!!!

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧