Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: C# on ASP.NET Page.

  1. #1

    C# on ASP.NET Page.

    I would like to use the following code to display it on an ASP.NET page.
    Currently this code is for a command line application, so I know I need to change Console.WriteLine to something else.
    I don't know how to put this on an ASP.NET page, I think I need to use the <script runat="server"> tag somehow though.

    Here's the code
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace cli
    {
        class Program
        {
            private const string FILE = @"xml/usersettings.xml";
            static void Main(string[] args)
            {
                try
                {
                    XmlDocument xml = new XmlDocument();
                    xml.Load(FILE);
                    XmlNodeList people = xml.GetElementsByTagName("person");
                    foreach (XmlNode node in people)
                    {
                        XmlElement person = (XmlElement)node;
    
                        string name = person.GetElementsByTagName("name")[0].InnerText;
                        int age = Convert.ToInt32(person.GetElementsByTagName("age")[0].InnerText);
                        string lang = person.GetElementsByTagName("lang")[0].InnerText;
                        
                        Console.WriteLine("Name: {0}", name.ToString());
                        Console.WriteLine("Age: {0}", age.ToString());
                        Console.WriteLine("language: {0}\n", lang.ToString());
                    }
       
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: \n\n {0}", e.ToString());
                    System.IO.StreamWriter err = new System.IO.StreamWriter(@"err.txt");
                    err.WriteLine("Error Message:\n\n{0}", e.ToString());
                    err.Close();
                }
            }
        }
    }
    edit
    This is also the XML being parsed, I thought I may aswell include it (I used peoples names here as I didn't know what else to put ha).
    HTML Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <usersettings>
      <person>
        <name>Alex</name>
        <age>14</age>
        <lang>PHP</lang>
      </person>
      <person>
        <name>Swagner</name>
        <age>14</age>
        <lang>HTML/CSS</lang>
      </person>
      <person>
        <name>Marc</name>
        <age>15</age>
        <lang>HTML/CSS/~PHP</lang>
      </person>
      <person>
        <name>Craig</name>
        <age>16</age>
        <lang>HTML</lang>
      </person>
    </usersettings>
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  2. #2
    Hey

    I am not a wiz at .net but here we go...

    1) You can replace the Console.WriteLine to Reponse.Write

    It would look like...
    Console.WriteLine("Name: {0}", name.ToString());
    Response.Write("Name: {0}", name.ToString());

    2) The <script runat="server"> is only needed for <asp:???> items. You would drag them from the Standard toolbox. You don't need the runat server for c# stuff.

    3) The is no MAIN in asp.net c# or vb.

    4) Here the coverted code behind page. The _Default means this is part of the Default.aspx webpage.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public
    partialclass_Default : System.Web.UI.Page
    {
    privateconststring FILE = @"xml/usersettings.xml";
    protectedvoid Page_Load(object sender, EventArgs e)
    {
    try
    {
    XmlDocument xml = newXmlDocument();
    xml.Load(FILE);
    XmlNodeList people = xml.GetElementsByTagName("person");
    foreach (XmlNode node in people)
    {
    XmlElement person = (XmlElement)node;
    string name = person.GetElementsByTagName("name")[0].InnerText;
    int age = Convert.ToInt32(person.GetElementsByTagName("age")[0].InnerText);
    string lang = person.GetElementsByTagName("lang")[0].InnerText;
    Response.Write(
    "Name: {0}", name.ToString());
    Response.Write(
    "Age: {0}", age.ToString());
    Response.Write(
    "language: {0}\n", lang.ToString());
    }
    }
    catch (Exception e)
    {
    Response.Write(
    "Error: \n\n {0}", e.ToString());
    System.IO.
    StreamWriter err = new System.IO.StreamWriter(@"err.txt");
    err.WriteLine(
    "Error Message:\n\n{0}", e.ToString());
    err.Close();
    }
    }
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012