Mostafa gh

Mostafa gh

  • NA
  • 3
  • 533

how repeat specific tag with child after last tag XML

Jan 9 2018 7:25 AM
hi
my xml samle:
 
  1. <Y>  
  2.   <x>  
  3.     <PH>  
  4.       <SQ>3SQ>  
  5.       <ND>4524115ND>  
  6.       <SN>545SN>  
  7.       <PC>545PC>  
  8.       <PG>54PG>  
  9.       <PP>5400PP>  
  10.       <PS>44500PS>  
  11.       <IS>0IS>  
  12.     PH>  
  13.     <BY>  
  14.       <MH>  
  15.         <MG>4545MG>  
  16.         <MP>25000MP>  
  17.         <MI>15000MI>  
  18.         <MS>0MS>  
  19.       MH>  
  20.     BY>  
  21.   x>  
  22. Y>  
 i have create text box for each tag and i have one button (save) to create X tag and chiled.
 
i want add new button "add MH" to add MH tag and child ( MG,MP,MI,MS) after last MH tag
i wang after click button update xml sample this:
 
  1. <Y>  
  2.   <x>  
  3.     <PH>  
  4.       <SQ>3SQ>  
  5.       <ND>4524115ND>  
  6.       <SN>545SN>  
  7.       <PC>545PC>  
  8.       <PG>54PG>  
  9.       <PP>5400PP>  
  10.       <PS>44500PS>  
  11.       <IS>0IS>  
  12.     PH>  
  13.     <BY>  
  14.       <MH>  
  15.         <MG>4545MG>  
  16.         <MP>25000MP>  
  17.         <MI>15000MI>  
  18.         <MS>0MS>  
  19.       MH>  
  20.       <MH>  
  21.         <MG>123MG>                i want add this MH tag 
  22.         <MP>5000MP>     
  23.         <MI>5000MI>  
  24.         <MS>0MS>  
  25.       MH>  
  26.     BY>  
  27.   x>  
  28. Y>  
 my code for Button Save is:
 
  1. private void button1_Click(object sender, EventArgs e){   
  2.  XmlNode customernode = xmldoc.CreateNode(XmlNodeType.Element, "x"null);  
  3.     XmlNode tartibnode = xmldoc.CreateNode(XmlNodeType.Element, "SQ"null);  
  4.     XmlNode datevisitnode = xmldoc.CreateNode(XmlNodeType.Element, "ND"null);  
  5.     XmlNode bimenode = xmldoc.CreateNode(XmlNodeType.Element, "SN"null);  
  6.     XmlNode nezamnode = xmldoc.CreateNode(XmlNodeType.Element, "PC"null);  
  7.     XmlNode pezeshknode = xmldoc.CreateNode(XmlNodeType.Element, "PG"null);  
  8.     XmlNode mablaghkolnode = xmldoc.CreateNode(XmlNodeType.Element, "PP"null);  
  9.     XmlNode mablaghbimarnode = xmldoc.CreateNode(XmlNodeType.Element, "PS"null);  
  10.     XmlNode mablaghbimenode = xmldoc.CreateNode(XmlNodeType.Element, "IS"null);  
  11.     XmlNode khedmatnode = xmldoc.CreateNode(XmlNodeType.Element, "MG"null);  
  12.     XmlNode sahmekolnode = xmldoc.CreateNode(XmlNodeType.Element, "MP"null);  
  13.     XmlNode sahmebimenode = xmldoc.CreateNode(XmlNodeType.Element, "MI"null);  
  14.     XmlNode sahmebimarnode = xmldoc.CreateNode(XmlNodeType.Element, "MS"null);  
  15.     XmlNode tagkolnode = xmldoc.CreateNode(XmlNodeType.Element, "PH"null);  
  16.     XmlNode tagkhedmatnode = xmldoc.CreateNode(XmlNodeType.Element, "MH"null);  
  17.     XmlNode tagbynode = xmldoc.CreateNode(XmlNodeType.Element, "BY"null);  
  18.   
  19.     tartibnode.InnerText = txtsq.Text;  
  20.     datevisitnode.InnerText = txtnd.Text;  
  21.     bimenode.InnerText = txtsn.Text;                  
  22.     nezamnode.InnerText = txtpc.Text;  
  23.     pezeshknode.InnerText = txtpg.Text;  
  24.     mablaghkolnode.InnerText = txtpp.Text;  
  25.     mablaghbimarnode.InnerText = txtps.Text;  
  26.     mablaghbimenode.InnerText = txtis.Text;  
  27.     khedmatnode.InnerText = txtmg.Text;  
  28.     sahmekolnode.InnerText = txtmp.Text;  
  29.     sahmebimenode.InnerText = txtmi.Text;  
  30.     sahmebimarnode.InnerText = txtms.Text;  
  31.   
  32. tagkolnode.AppendChild(tartibnode);  
  33. tagkolnode.AppendChild(datevisitnode);  
  34. tagkolnode.AppendChild(bimenode);  
  35. tagkolnode.AppendChild(nezamnode);  
  36. tagkolnode.AppendChild(pezeshknode);  
  37. tagkolnode.AppendChild(mablaghkolnode);  
  38. tagkolnode.AppendChild(mablaghbimarnode);  
  39. tagkolnode.AppendChild(mablaghbimenode);  
  40.   
  41. tagkhedmatnode.AppendChild(khedmatnode);  
  42. tagkhedmatnode.AppendChild(sahmekolnode);  
  43. tagkhedmatnode.AppendChild(sahmebimenode);  
  44. tagkhedmatnode.AppendChild(sahmebimarnode);  
  45.   
  46.     tagbynode.AppendChild(tagkhedmatnode);  
  47.     customernode.AppendChild(tagkolnode);  
  48.     customernode.AppendChild(tagbynode);  
  49.     rootnote.AppendChild(customernode);  
  50.   
  51.     xmldoc.Save(xmlpath);  }
 for Button update this code but not work:
  1. private void button2_Click(object sender, EventArgs e)  
  2.        {  
  3.   
  4.            xmldoc.Load(xmlpath);  
  5.            rootnote = xmldoc.DocumentElement;  
  6.   
  7.            XmlNode khedmatnode = xmldoc.CreateNode(XmlNodeType.Element, "MG"null);  
  8.            XmlNode sahmekolnode = xmldoc.CreateNode(XmlNodeType.Element, "MP"null);  
  9.            XmlNode sahmebimenode = xmldoc.CreateNode(XmlNodeType.Element, "MI"null);  
  10.            XmlNode sahmebimarnode = xmldoc.CreateNode(XmlNodeType.Element, "MS"null);              
  11.            XmlNode tagkhedmatnode = xmldoc.CreateNode(XmlNodeType.Element, "MH"null);  
  12.            XmlNode tagbynode = xmldoc.CreateNode(XmlNodeType.Element, "BY"null);  
  13.   
  14.   
  15.            khedmatnode.InnerText = txtmg.Text;  
  16.            sahmekolnode.InnerText = txtmp.Text;  
  17.            sahmebimenode.InnerText = txtmi.Text;  
  18.            sahmebimarnode.InnerText = txtms.Text;  
  19.   
  20.            tagkhedmatnode.AppendChild(khedmatnode);  
  21.            tagkhedmatnode.AppendChild(sahmekolnode);  
  22.            tagkhedmatnode.AppendChild(sahmebimenode);  
  23.            tagkhedmatnode.AppendChild(sahmebimarnode);  
  24.   
  25.            tagbynode.AppendChild(tagkhedmatnode);  
  26.              
  27.            tagbynode.InsertAfter(tagkhedmatnode , tagkhedmatnode);  
  28.          
  29.   
  30.   
  31.            xmldoc.Save(xmlpath);  
  32.        }  
 tank you
 

Answers (1)