如何使用c#替换Xml属性前缀值


<ce_table frame="topbot" id="t0010" rowsep="0" colsep="0">   
<ce_label>Table 2</ce_label> 
<ce_caption id="cn040"> 
<ce_simple-para id="spar055">Model fit cnbs for the span targeted moments.</ce_simple-para> 
</ce_caption>
</ce_label>  
</ce_table>

我需要将id="t0010"改为id="tf0010",id="cn.. "改为id="cib.. "

我只需要改变一个属性值的前缀。

使用xml linq:

using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement ce_table = doc.Descendants("ce_table").First();
ce_table.SetAttributeValue("id", "tf0010");
XElement ce_caption = ce_table.Descendants("ce_caption").First();
ce_caption.SetAttributeValue("id", "cib040");


}

}

}

最新更新