Create XML file with C# or ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NegarZaeem
    New Member
    • Aug 2008
    • 4

    Create XML file with C# or ASP

    I want to creat this XML file with C# or ASP
    Code:
    <?xml version="1.0" standalone="yes"?>
    <Info>
      <News>
        <Item>
          <Id>1</Id>
          <Title>Test1</Title>
          <Body>testing1 testing1 testing1</Body>
        </Item>
        <Item>
          <Id>2</Id>
          <Title>Test2</Title>
          <Body>testing2 testing2 testing2</Body>
        </Item>
        <Item>
          <Id>3</Id>
          <Title>Test3</Title>
          <Body>testing3 testing3 testing3</Body>
        </Item>
      </News>
      <Press>
        <Item>
          <Id>1</Id>
          <Title>t1</Title>
        </Item>
        <Item>
          <Id>2</Id>
          <Title>t2</Title>
        </Item>
      </Press>
    </Info>
    I have done this:

    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    
    namespace test6
    {
        public partial class _Default : System.Web.UI.Page
        {
            DataSet ds = new DataSet("Info");
            DataTable dt = new DataTable("News");
         
            protected void Page_Load(object sender, EventArgs e)
            {
                DataColumn dc = new DataColumn("ID");
                DataColumn dc1 = new DataColumn("Title");
     
                dt.Columns.Add(dc);
                dt.Columns.Add(dc1);
           
                dt.Merge(dt);
                ds.Tables.Add(dt);
      
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                test();
            }
          
            private void test()
            {
    
                DataRow dr = ds.Tables["News"].NewRow();
                dr["ID"] = TextBox1.Text;
                dr["Title"] = TextBox2.Text;
                ds.Tables["News"].Rows.Add(dr);
                ds.WriteXml(Server.MapPath(Request.ApplicationPath) + "DB.xml");
                Label1.Text = "testing testing testing";
        
            }
    
        }
    }
    but the Result is:

    Code:
    <?xml version="1.0" standalone="yes"?>
    <Info>
      <News>
        <ID>1</ID>
        <Title>d</Title>
      </News>
    </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?
    Last edited by kenobewan; Aug 31 '08, 12:05 PM. Reason: Please use code tags
  • NegarZaeem
    New Member
    • Aug 2008
    • 4

    #2
    creating a sprecial xml file

    I want to creat this XML file with C# or ASP
    [CODE=xml]<?xml version="1.0" standalone="yes "?>
    <Info>
    <News>
    <Item>
    <Id>1</Id>
    <Title>Test1</Title>
    <Body>testing 1 testing1 testing1</Body>
    </Item>
    <Item>
    <Id>2</Id>
    <Title>Test2</Title>
    <Body>testing 2 testing2 testing2</Body>
    </Item>
    <Item>
    <Id>3</Id>
    <Title>Test3</Title>
    <Body>testing 3 testing3 testing3</Body>
    </Item>
    </News>
    <Press>
    <Item>
    <Id>1</Id>
    <Title>t1</Title>
    </Item>
    <Item>
    <Id>2</Id>
    <Title>t2</Title>
    </Item>
    </Press>
    </Info>
    [/CODE]
    I have done this:

    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    
    namespace test6
    {
        public partial class _Default : System.Web.UI.Page
        {
            DataSet ds = new DataSet("Info");
            DataTable dt = new DataTable("News");
         
            protected void Page_Load(object sender, EventArgs e)
            {
                DataColumn dc = new DataColumn("ID");
                DataColumn dc1 = new DataColumn("Title");
     
                dt.Columns.Add(dc);
                dt.Columns.Add(dc1);
           
                dt.Merge(dt);
                ds.Tables.Add(dt);
      
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                test();
            }
          
            private void test()
            {
    
                DataRow dr = ds.Tables["News"].NewRow();
                dr["ID"] = TextBox1.Text;
                dr["Title"] = TextBox2.Text;
                ds.Tables["News"].Rows.Add(dr);
                ds.WriteXml(Server.MapPath(Request.ApplicationPath) + "DB.xml");
                Label1.Text = "testing testing testing";
        
            }
    
        }
    }
    but the Result is:

    [CODE=xml]<?xml version="1.0" standalone="yes "?>
    <Info>
    <News>
    <ID>1</ID>
    <Title>d</Title>
    </News>
    </Info>
    [/CODE]
    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?
    Last edited by acoder; Sep 3 '08, 05:54 PM. Reason: Added [code] tags

    Comment

    • NegarZaeem
      New Member
      • Aug 2008
      • 4

      #3
      How can I create this special xml file

      I want to creat this XML file with C# or ASP
      [code=xml]<?xml version="1.0" standalone="yes "?>
      <Info>
      <News>
      <Item>
      <Id>1</Id>
      <Title>Test1</Title>
      <Body>testing 1 testing1 testing1</Body>
      </Item>
      <Item>
      <Id>2</Id>
      <Title>Test2</Title>
      <Body>testing 2 testing2 testing2</Body>
      </Item>
      <Item>
      <Id>3</Id>
      <Title>Test3</Title>
      <Body>testing 3 testing3 testing3</Body>
      </Item>
      </News>
      <Press>
      <Item>
      <Id>1</Id>
      <Title>t1</Title>
      </Item>
      <Item>
      <Id>2</Id>
      <Title>t2</Title>
      </Item>
      </Press>
      </Info>[/code]

      I have done this:

      [code=cs]using System;
      using System.Collecti ons;
      using System.Configur ation;
      using System.Data;
      using System.Web;
      using System.Web.Secu rity;
      using System.Web.UI;
      using System.Web.UI.H tmlControls;
      using System.Web.UI.W ebControls;
      using System.Web.UI.W ebControls.WebP arts;
      using System.IO;
      using System.Xml;
      using System.Xml.Sche ma;
      using System.Xml.Seri alization;

      namespace test6
      {
      public partial class _Default : System.Web.UI.P age
      {
      DataSet ds = new DataSet("Info") ;
      DataTable dt = new DataTable("News ");

      protected void Page_Load(objec t sender, EventArgs e)
      {
      DataColumn dc = new DataColumn("ID" );
      DataColumn dc1 = new DataColumn("Tit le");

      dt.Columns.Add( dc);
      dt.Columns.Add( dc1);

      dt.Merge(dt);
      ds.Tables.Add(d t);

      }

      protected void Button1_Click(o bject sender, EventArgs e)
      {
      test();
      }

      private void test()
      {

      DataRow dr = ds.Tables["News"].NewRow();
      dr["ID"] = TextBox1.Text;
      dr["Title"] = TextBox2.Text;
      ds.Tables["News"].Rows.Add(dr);
      ds.WriteXml(Ser ver.MapPath(Req uest.Applicatio nPath) + "DB.xml");
      Label1.Text = "testing testing testing";

      }

      }
      }[/code]

      but the Result is:

      [code=xml]<?xml version="1.0" standalone="yes "?>
      <Info>
      <News>
      <ID>1</ID>
      <Title>d</Title>
      </News>
      </Info>[/code]

      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?
      Last edited by jhardman; Sep 3 '08, 05:32 PM. Reason: Moved to .NET forum. ASP forum is for "classic" ASP only

      Comment

      • DonBytes
        New Member
        • Aug 2008
        • 25

        #4
        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.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          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.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

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

            Moderator.

            Comment

            Working...