473,503 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create XML file with C# or ASP

4 New Member
I want to creat this XML file with C# or ASP
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <Info>
  3.   <News>
  4.     <Item>
  5.       <Id>1</Id>
  6.       <Title>Test1</Title>
  7.       <Body>testing1 testing1 testing1</Body>
  8.     </Item>
  9.     <Item>
  10.       <Id>2</Id>
  11.       <Title>Test2</Title>
  12.       <Body>testing2 testing2 testing2</Body>
  13.     </Item>
  14.     <Item>
  15.       <Id>3</Id>
  16.       <Title>Test3</Title>
  17.       <Body>testing3 testing3 testing3</Body>
  18.     </Item>
  19.   </News>
  20.   <Press>
  21.     <Item>
  22.       <Id>1</Id>
  23.       <Title>t1</Title>
  24.     </Item>
  25.     <Item>
  26.       <Id>2</Id>
  27.       <Title>t2</Title>
  28.     </Item>
  29.   </Press>
  30. </Info>
I have done this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15.  
  16. namespace test6
  17. {
  18.     public partial class _Default : System.Web.UI.Page
  19.     {
  20.         DataSet ds = new DataSet("Info");
  21.         DataTable dt = new DataTable("News");
  22.  
  23.         protected void Page_Load(object sender, EventArgs e)
  24.         {
  25.             DataColumn dc = new DataColumn("ID");
  26.             DataColumn dc1 = new DataColumn("Title");
  27.  
  28.             dt.Columns.Add(dc);
  29.             dt.Columns.Add(dc1);
  30.  
  31.             dt.Merge(dt);
  32.             ds.Tables.Add(dt);
  33.  
  34.         }
  35.  
  36.         protected void Button1_Click(object sender, EventArgs e)
  37.         {
  38.             test();
  39.         }
  40.  
  41.         private void test()
  42.         {
  43.  
  44.             DataRow dr = ds.Tables["News"].NewRow();
  45.             dr["ID"] = TextBox1.Text;
  46.             dr["Title"] = TextBox2.Text;
  47.             ds.Tables["News"].Rows.Add(dr);
  48.             ds.WriteXml(Server.MapPath(Request.ApplicationPath) + "DB.xml");
  49.             Label1.Text = "testing testing testing";
  50.  
  51.         }
  52.  
  53.     }
  54. }
but the Result is:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <Info>
  3.   <News>
  4.     <ID>1</ID>
  5.     <Title>d</Title>
  6.   </News>
  7. </Info>
I know that one of the problems is that everytime that I run the the code, a new table will be created so that a new table with new data will be replaced to the file

what can I do to creat that xml file?
Aug 31 '08 #1
5 1299
NegarZaeem
4 New Member
I want to creat this XML file with C# or ASP
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <Info>
  3.   <News>
  4.     <Item>
  5.       <Id>1</Id>
  6.       <Title>Test1</Title>
  7.       <Body>testing1 testing1 testing1</Body>
  8.     </Item>
  9.     <Item>
  10.       <Id>2</Id>
  11.       <Title>Test2</Title>
  12.       <Body>testing2 testing2 testing2</Body>
  13.     </Item>
  14.     <Item>
  15.       <Id>3</Id>
  16.       <Title>Test3</Title>
  17.       <Body>testing3 testing3 testing3</Body>
  18.     </Item>
  19.   </News>
  20.   <Press>
  21.     <Item>
  22.       <Id>1</Id>
  23.       <Title>t1</Title>
  24.     </Item>
  25.     <Item>
  26.       <Id>2</Id>
  27.       <Title>t2</Title>
  28.     </Item>
  29.   </Press>
  30. </Info>
  31.  
I have done this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15.  
  16. namespace test6
  17. {
  18.     public partial class _Default : System.Web.UI.Page
  19.     {
  20.         DataSet ds = new DataSet("Info");
  21.         DataTable dt = new DataTable("News");
  22.  
  23.         protected void Page_Load(object sender, EventArgs e)
  24.         {
  25.             DataColumn dc = new DataColumn("ID");
  26.             DataColumn dc1 = new DataColumn("Title");
  27.  
  28.             dt.Columns.Add(dc);
  29.             dt.Columns.Add(dc1);
  30.  
  31.             dt.Merge(dt);
  32.             ds.Tables.Add(dt);
  33.  
  34.         }
  35.  
  36.         protected void Button1_Click(object sender, EventArgs e)
  37.         {
  38.             test();
  39.         }
  40.  
  41.         private void test()
  42.         {
  43.  
  44.             DataRow dr = ds.Tables["News"].NewRow();
  45.             dr["ID"] = TextBox1.Text;
  46.             dr["Title"] = TextBox2.Text;
  47.             ds.Tables["News"].Rows.Add(dr);
  48.             ds.WriteXml(Server.MapPath(Request.ApplicationPath) + "DB.xml");
  49.             Label1.Text = "testing testing testing";
  50.  
  51.         }
  52.  
  53.     }
  54. }
  55.  
but the Result is:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <Info>
  3.   <News>
  4.     <ID>1</ID>
  5.     <Title>d</Title>
  6.   </News>
  7. </Info>
  8.  
I know that one of the problems is that everytime that I run the the code, a new table will be created so that a new table with new data will be replaced to the file

what can i do to creat that xml file?
Aug 31 '08 #2
NegarZaeem
4 New Member
I want to creat this XML file with C# or ASP
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <Info>
  3.   <News>
  4.     <Item>
  5.       <Id>1</Id>
  6.       <Title>Test1</Title>
  7.       <Body>testing1 testing1 testing1</Body>
  8.     </Item>
  9.     <Item>
  10.       <Id>2</Id>
  11.       <Title>Test2</Title>
  12.       <Body>testing2 testing2 testing2</Body>
  13.     </Item>
  14.     <Item>
  15.       <Id>3</Id>
  16.       <Title>Test3</Title>
  17.       <Body>testing3 testing3 testing3</Body>
  18.     </Item>
  19.   </News>
  20.   <Press>
  21.     <Item>
  22.       <Id>1</Id>
  23.       <Title>t1</Title>
  24.     </Item>
  25.     <Item>
  26.       <Id>2</Id>
  27.       <Title>t2</Title>
  28.     </Item>
  29.   </Press>
  30. </Info>
I have done this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15.  
  16. namespace test6
  17. {
  18.     public partial class _Default : System.Web.UI.Page
  19.     {
  20.         DataSet ds = new DataSet("Info");
  21.         DataTable dt = new DataTable("News");
  22.  
  23.         protected void Page_Load(object sender, EventArgs e)
  24.         {
  25.             DataColumn dc = new DataColumn("ID");
  26.             DataColumn dc1 = new DataColumn("Title");
  27.  
  28.             dt.Columns.Add(dc);
  29.             dt.Columns.Add(dc1);
  30.  
  31.             dt.Merge(dt);
  32.             ds.Tables.Add(dt);
  33.  
  34.         }
  35.  
  36.         protected void Button1_Click(object sender, EventArgs e)
  37.         {
  38.             test();
  39.         }
  40.  
  41.         private void test()
  42.         {
  43.  
  44.             DataRow dr = ds.Tables["News"].NewRow();
  45.             dr["ID"] = TextBox1.Text;
  46.             dr["Title"] = TextBox2.Text;
  47.             ds.Tables["News"].Rows.Add(dr);
  48.             ds.WriteXml(Server.MapPath(Request.ApplicationPath) + "DB.xml");
  49.             Label1.Text = "testing testing testing";
  50.  
  51.         }
  52.  
  53.     }
  54. }
but the Result is:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <Info>
  3.   <News>
  4.     <ID>1</ID>
  5.     <Title>d</Title>
  6.   </News>
  7. </Info>
I know that one of the problems is that everytime that I run the the code, a new table will be created so that a new table with new data will be replaced to the file

what can i do to creat that xml file?
Aug 31 '08 #3
DonBytes
25 New Member
I believe you'd have to create it yourself using XmlDocument and CreateElement for each row/column. You can save it and then load it (if it exists) prior to adding items.

I really don't see any other way of getting the extra level (<item>) in there neither.
Aug 31 '08 #4
Dormilich
8,658 Recognized Expert Moderator Expert
Hi, maybe this thread is placed better in the C# or ASP section (for I don't know any of those languages).
You can ask a moderator to move it for you.
Sep 1 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
Threads have been merged. Please use code tags when posting code.

Moderator.
Sep 3 '08 #6

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

Similar topics

4
6756
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to...
7
8821
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
4382
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
5
6725
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
8
20327
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1:...
23
7366
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...
4
6875
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
3914
by: sanghavi | last post by:
how to create a set up project in vb.net..how to run an application on a different machine
3
2036
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
15
5237
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
7199
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
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6982
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...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5000
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...
0
3161
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...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
374
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...

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.