Connecting Tech Pros Worldwide Help | Site Map

Create XML file with C# or ASP

Newbie
 
Join Date: Aug 2008
Posts: 3
#1: Aug 31 '08
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?
Newbie
 
Join Date: Aug 2008
Posts: 3
#2: Aug 31 '08

re: Create XML file with C# or ASP


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?
Newbie
 
Join Date: Aug 2008
Posts: 3
#3: Aug 31 '08

re: Create XML file with C# or ASP


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?
Newbie
 
Join Date: Aug 2008
Posts: 25
#4: Aug 31 '08

re: Create XML file with C# or ASP


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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#5: Sep 1 '08

re: Create XML file with C# or ASP


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.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Sep 3 '08

re: Create XML file with C# or ASP


Threads have been merged. Please use code tags when posting code.

Moderator.
Reply