1

Can Somebody please help me with the below codes, I really appreciates anyone help.

Below are my C# codes, the purpose of this code is to replace the attributes inside the XML file

protected void GenerateXML(object sender, EventArgs e)
{
    string path;
    string txtfile;
    path = Server.MapPath("~\\Temp\\myxml.xml");
    string dpath = Server.MapPath("~\\Download\\");
    txtfile = File.ReadAllText(path);
    foreach (GridViewRow srow in Main.Rows)
    {
        string batype = srow.Cells[0].Text;

        string apath = dpath + "XML_Report";
        if (!Directory.Exists(apath))
            Directory.CreateDirectory(apath);
        string Outxmlfile = apath + ".xml";
        if (!File.Exists(Outxmlfile))
        {
            using (StreamWriter txtsw = File.CreateText(Outxmlfile))
            {
                txtsw.WriteLine(txtfile);
            }
            txtfile = txtfile.Replace(("<celName>"), batype);

            File.WriteAllText(Outxmlfile, txtfile);
        }
    }
}

Below are my codes for my Gridview named as "Main"

<div class="tabContents">   
    <asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
        <asp:View ID="View1" runat="server">
            <asp:GridView ID="Main" runat="server" AllowUserToAddRows="False"  HorizontalAlign="Left" Font-Bold="True" Height="10px" AllowSorting="True">
                <HeaderStyle BackColor="#FF9966" Font-Bold="True" Font-Size="Large" ForeColor="White" Wrap="False" />
            </asp:GridView>
        </asp:View>
    </asp:MultiView>
</div>

The template I used is myxml.xml containing below elements:

<mainelement>
  <p name="BatteryCellName"><celName></p>
  <p name="BatteryCellName"><celName></p>
  <p name="BatteryCellName"><celName></p>     
</mainelement>

I wanted to replace the attribute celName with the one showing in the Gridview enter image description here

I want the output to be below:

<mainelement>
  <p name="BatteryCellName">Lithium-ion</p>
  <p name="BatteryCellName">Nickel Cadmium</p>
  <p name="BatteryCellName">Nickel-Metal Hydride</p>      
</mainelement>

But everytime I run my c# could it will display only the Lithium-ion. Can somebody help me I'm a newbie:-)

<mainelement>
  <p name="BatteryCellName">Lithium-ion</p>
  <p name="BatteryCellName">Lithium-ion</p>
  <p name="BatteryCellName">Lithium-ion</p>   
</mainelement>
Orkad
  • 630
  • 5
  • 15
Lacky lac
  • 11
  • 2
  • 1
    that is NOT xml! – Mario Vernari Aug 07 '20 at 05:12
  • 1
    Try iterate over the array instead of hard-coding the index (i.e. `srow.Cells[0].Text`) would probably solve the problem. – Zephyr Aug 07 '20 at 05:51
  • 1
    `txtFile.Replace("", batype)` will replace all three occurrences of in the first loop iteration. See also https://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net – Klaus Gütter Aug 07 '20 at 07:12
  • 1
    Thanks for the help so far nothing works for me not sure if there is any other approach. – Lacky lac Aug 07 '20 at 13:36

0 Answers0