Table 2 Model fit" /> Table 2 Model fit" /> Table 2 Model fit"/>

How to replace Xml Attribute Prefix value by using C#

49 Views Asked by At
<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>

I need to change id="t0010" to id="tf0010" and id="cn.. " to id="cib.. ".

I only need to change prefix of an attribute value.

1

There are 1 best solutions below

0
jdweng On

Use xml linq :

using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.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");
 


        }
 
    }
 
}