473,781 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create xml file from c# for loop.

122 New Member
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 5250
DragonLord
122 New Member
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 New Member
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
1466
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
8869
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 want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
4
2667
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 can avoid problems with it in the future. If there are any references I should check out let me know. Full highlighted code at:
4
11338
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. Thanks in advance.
23
7417
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
2
2404
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 all the files and folders so that it would map the file system hierarchy.
4
6916
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 function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
3
7194
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 if there is a way to use the information in this table to create the many tables that are listed in the source table instead of creating each table individually? Many thanks for any help you can offer.
0
3738
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 had to explore the internet in order to discover various tutorials and examples that have led me on a positive path. Right now I am working on a Google Mash-Up that will incorporate over 14,000 records, which will appear as separate markers that...
0
1467
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 1. <span class="style2"> 2. <style type="text/css"> 3. <!-- 4. body, th, td, p, small {
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.