468,121 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

Create xml file from c# for loop.

122 100+
Ok basically this is mostly working,

What i want to do is create an xml file with multiple updates that have the same attribute.

<root>
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>
</root>


here is the code i have so far the thing is i end up with one
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>

with the values all concatinated together so instead of

<update>
<element1>1</element1>
<element2>1</element2>
<element3>1</element3>
<element4>1</element4>
</update>
<update>
<element1>2</element1>
<element2>2</element2>
<element3>2</element3>
<element4>2</element4>
</update>
Oct 10 '08 #1
2 4423
DragonLord
122 100+
Ok basically this is mostly working,

What i want to do is create an xml file with multiple updates that have the same attribute.

<root>
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>
</root>


here is the code i have so far the thing is i end up with one
<update>
<element1>value</element1>
<element2>value</element2>
<element3>value</element3>
<element4>value</element4>
</update>

with the values all concatinated together so instead of

<update>
<element1>1</element1>
<element2>1</element2>
<element3>1</element3>
<element4>1</element4>
</update>
<update>
<element1>2</element1>
<element2>2</element2>
<element3>2</element3>
<element4>2</element4>
</update>


I get
<update>
<element1>12</element1>
<element2>12</element2>
<element3>12</element3>
<element4>12</element4>
</update>

can anyone tell me what i need to change to make it work as desired?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10.  
  11. namespace xmlTest
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             try
  23.             {
  24.                 //pick whatever filename with .xml extension
  25.                 //string filename = "C:\\XML" + DateTime.Now. + ".xml";
  26.                 //string filename = "C:\\POD" + DateTime.Now.ToString("MMddyyhhmmss") + ".xml";
  27.                 string filename = "C:\\POD.xml";
  28.                 XmlDocument xmlDoc = new XmlDocument();
  29.  
  30.                 try
  31.                 {
  32.                     xmlDoc.Load(filename);
  33.                 }
  34.                 catch (System.IO.FileNotFoundException)
  35.                 {
  36.                     //if file is not found, create a new xml file
  37.                     XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
  38.                     xmlWriter.Formatting = Formatting.Indented;
  39.                     xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
  40.                     xmlWriter.WriteStartElement("InfoLinkDocument");
  41.                     //If WriteProcessingInstruction is used as above,
  42.                     //Do not use WriteEndElement() here
  43.                     //xmlWriter.WriteEndElement();
  44.                     //it will cause the <Root></Root> to be <Root />
  45.                     xmlWriter.Close();
  46.                     xmlDoc.Load(filename);
  47.                 }
  48.  
  49.                 //Setup files nodes and elements.
  50.                 XmlNode root = xmlDoc.DocumentElement;
  51.                 //XmlNode HAWUpdate = xmlDoc.DocumentElement;
  52.  
  53.                 //Header File
  54.                 XmlElement AccessRequest = xmlDoc.CreateElement("AccessRequest");
  55.                 XmlElement DocumentType = xmlDoc.CreateElement("DocumentType");
  56.                 XmlElement EntityID = xmlDoc.CreateElement("EntityID");
  57.                 XmlElement EntityPIN = xmlDoc.CreateElement("EntityPIN");
  58.                 XmlElement Version = xmlDoc.CreateElement("Version");
  59.                 XmlElement TimeStamp = xmlDoc.CreateElement("TimeStamp");
  60.                 XmlElement NotifyOnSuccess = xmlDoc.CreateElement("NotifyOnSuccess");
  61.                 XmlElement ReplyEmailAddress = xmlDoc.CreateElement("ReplyEmailAddress");
  62.  
  63.                 //HawUpdate section
  64.                 XmlElement HUpdate = xmlDoc.CreateElement("HAWUpdate");
  65.                 XmlElement HAWBNumber = xmlDoc.CreateElement("HAWBNumber");
  66.                 XmlElement UpdateEntity = xmlDoc.CreateElement("UpdateEntity");
  67.                 XmlElement PINumber = xmlDoc.CreateElement("PINumber");
  68.                 XmlElement Pieces = xmlDoc.CreateElement("Pieces");
  69.                 XmlElement ActionDate = xmlDoc.CreateElement("ActionDate");
  70.                 XmlElement Signature = xmlDoc.CreateElement("Comment1");
  71.                 XmlElement SourceID = xmlDoc.CreateElement("SourceID");
  72.  
  73.                 XmlText textNode = xmlDoc.CreateTextNode("hello");
  74.                 XmlText textNode2 = xmlDoc.CreateTextNode("secondtext");
  75.                 XmlText textNode3 = xmlDoc.CreateTextNode("ThirdNode");
  76.                 textNode2.Value = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss");
  77.                 textNode.Value = "hello, world";
  78.  
  79.                 //Create the xml file structure.
  80.                 root.AppendChild(AccessRequest);
  81.                 AccessRequest.AppendChild(DocumentType);
  82.                 XmlText textNodeDT = xmlDoc.CreateTextNode("textNodeDT");
  83.                 textNodeDT.Value = "3";
  84.                 AccessRequest.AppendChild(EntityID);
  85.                 XmlText textNodeEID = xmlDoc.CreateTextNode("textNodeEID");
  86.                 textNodeEID.Value = "3994";
  87.                 AccessRequest.AppendChild(EntityPIN);
  88.                 XmlText textNodeEPIN = xmlDoc.CreateTextNode("textNodeEPIN");
  89.                 textNodeEPIN.Value = "xx3994";
  90.                 AccessRequest.AppendChild(Version);
  91.                 XmlText textNodeV = xmlDoc.CreateTextNode("textNodeV");
  92.                 textNodeV.Value = "1.2";
  93.                 AccessRequest.AppendChild(TimeStamp);
  94.                 XmlText textNodeTS = xmlDoc.CreateTextNode("textNodeTS");
  95.                 textNodeTS.Value = "1.2";
  96.                 AccessRequest.AppendChild(NotifyOnSuccess);
  97.                 XmlText textNodeNos= xmlDoc.CreateTextNode("textNodeNoS");
  98.                 textNodeNos.Value = "1";
  99.                 AccessRequest.AppendChild(ReplyEmailAddress);
  100.                 XmlText textNodeAR = xmlDoc.CreateTextNode("textNodeAR");
  101.                 textNodeAR.Value = "Test 2";
  102.  
  103.  
  104.  
  105.                 //Add the data to the Headerfile
  106.                 DocumentType.AppendChild(textNodeDT);
  107.                 EntityID.AppendChild(textNodeEID);
  108.                 Version.AppendChild(textNodeV);
  109.                 NotifyOnSuccess.AppendChild(textNodeNos);
  110.  
  111.  
  112.                 for (int i = 0; i < 3; i++)
  113.                 {
  114.  
  115.                 //HAWUpdate.CloneNode(true);
  116.                 HUpdate.CloneNode(true);
  117.                 root.AppendChild(HUpdate);
  118.                   //HAWUpdate.AppendChild(HUpdate);
  119.  
  120.                 HUpdate.AppendChild(HAWBNumber);
  121.                 XmlText textNodeHN = xmlDoc.CreateTextNode("textNodeHN");
  122.                 textNodeHN.Value = i.ToString();
  123.                 HAWBNumber.AppendChild(textNodeHN);
  124.  
  125.                 HUpdate.AppendChild(UpdateEntity);
  126.                 XmlText textNodeHUE = xmlDoc.CreateTextNode("textNodeHUE");
  127.                 textNodeHUE.Value = i.ToString();
  128.                 UpdateEntity.AppendChild(textNodeHUE);
  129.  
  130.                 HUpdate.AppendChild(PINumber);
  131.                 XmlText textNodeHPIN = xmlDoc.CreateTextNode("textNodeHPIN");
  132.                 textNodeHPIN.Value = i.ToString();
  133.                 PINumber.AppendChild(textNodeHPIN);
  134.  
  135.  
  136.                 HUpdate.AppendChild(Pieces);
  137.                 XmlText textNodePieces = xmlDoc.CreateTextNode("textNodePieces");
  138.                 textNodePieces.Value = i.ToString();
  139.                 Pieces.AppendChild(textNodePieces);
  140.  
  141.                 HUpdate.AppendChild(ActionDate);
  142.                 XmlText textNodeHAD = xmlDoc.CreateTextNode("textNodeHAD");
  143.                 textNodeHAD.Value = i.ToString();
  144.                 ActionDate.AppendChild(textNodeHAD);
  145.  
  146.                 HUpdate.AppendChild(Signature);
  147.                 XmlText textNodeHS = xmlDoc.CreateTextNode("textNodeHS");
  148.                 textNodeHS.Value = i.ToString();
  149.                 Signature.AppendChild(textNodeHS);
  150.  
  151.                 HUpdate.AppendChild(SourceID);
  152.                 XmlText textNodeHSID = xmlDoc.CreateTextNode("textNodeHSID");
  153.                 textNodeHSID.Value = i.ToString();
  154.                 SourceID.AppendChild(textNodeHSID);
  155.  
  156.                 //xmlDoc.InsertAfter(HUpdate, xmlDoc.LastChild);
  157.                 xmlDoc.Save(filename);
  158.                 }
  159.  
  160.  
  161.  
  162.             }
  163.  
  164.             catch (Exception ex)
  165.             {
  166.                 WriteError(ex.ToString());
  167.             }
  168.         }
  169.         public void WriteError(string str)
  170.         {
  171.             MessageBox.Show(str);
  172.         }
  173.  
  174.     }
  175. }
  176.  
Oct 10 '08 #2
DragonLord
122 100+
Sorry for the double reply, anyone have any ideas?
Oct 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by FloWo3 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.