473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5186
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

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

Similar topics

10
by: FloWo3 | last post by:
Hi, how do you create (design) algorithms. I'm talking about thinking about it and come to an idea. Tanks, FroxX
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: Jeff Rodriguez | last post by:
Main just loops over this while it's not null. The segfault occurs at this line: *line = (char)ch; Also, please don't just fix the code. I would like to know why exactly this isn't working so I...
4
by: Abdhul Saleem | last post by:
Hi, I am recieving error ActiveX component can't create object in the following line in the asp page. set ExcelApp = CreateObject("Excel.Application") Previously this code was working fine....
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
by: TrevRex | last post by:
Hello, I work for a non-profit in San Diego as a GIS Specialist. I have had to teach myself about some scripting to create some dynamic maps, but I am still very limited in my skills, so I have...
0
by: tiingshii | last post by:
i have this working ----- http://pddesignstudio.com/ckc/php2 now i want to create spacing between each school how do i do it? can anyone help mi its my first time dealing with php PHP CODE ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.