tags:

views:

99

answers:

3

I'm planning to create a news item which uses xml as it's backend and the Display should be like:

Date: 08/Mar/2010
------------------------------
Title     | News
------------------------------
News 4 | Some news
News 3 | Some news
News 2 | Some news
News 1 | Some news
------------------------------
Date: 07/Mar/2010
------------------------------
Title     | News
------------------------------
News 5 | Some news
News 4 | Some news
News 3 | Some news
News 2 | Some news
News 1 | Some news

  1. Display should be sorted on Date (descending)
  2. Then news items should be sorted on time (descending)

Today's news item should be on top, then titles should be sorted-decending (timewise), later will come previous day's news items.

I'm not able to come up with the logic of xml which should be used in this case. moreover I'm not able to figure out how should I check "Today's date" in xml's "if" statement. Can I please get some code sample to understand this kinda logic???

---- Previous Question ------------------------------------------------------------------
How can I export a data from textBox1, textBox2 & textBox3 on my winform (visual studio C#) so that it can automatically create an xml file with proper placing of these data????
Let's say:
textBox1 = Name:
textBox2 = Age:
textBox3 = Roll No

It would be great if the exported xml can be appended (add the new data in the EOF) if we export the data again.

Any idea plz.....

+2  A: 

There are numerous ways of creating XML files in .NET. You could use object serialization , XmlWriter, XDocument, ...

Here's an example:

new XDocument(
    new XElement("user",
        new XElement("name", textBox1.Text),
        new XElement("age", textBox2.Text),
        new XElement("rollNo", textBox3.Text)
    )
).Save("user.xml");

Could generate an XML file that looks like that:

<?xml version="1.0" encoding="utf-8"?>
<user>
  <name>foo</name>
  <age>20</age>
  <rollNo>123</rollNo>
</user>
Darin Dimitrov
You will need to add a reference to the `System.Xml.Linq` assembly in your project.
Darin Dimitrov
Thx for the reply I found the problem. I'm having framework V3.0 whereas this assembly comes as a part of V3.5 No worries I had already downloaded the installer and manually added the assembly. :)
gsvirdi
I wanted a bit of more help for knowing about the "Append" function for xml so that the winForm should simply add all output data in the end of existing xml file.
gsvirdi
A: 

There are a million ways to do this. The approach I'd take in the situation you described:

  1. Build an class whose properties are the data elements I need to collect.
  2. Either use XML serialization (if I'm not conforming to a specific schema, this is the approach requiring the least code) or build a method into the class that used XDocument or XmlWriter to produce the XML.
  3. Write test cases to test the class and its XML-generation method.
  4. Build the form.
  5. Bind the UI controls to the properties of an instance of my class.

What I wouldn't do: Write a method in the form that pulls data out of the form controls and writes them to XML.

Robert Rossney
A: 

Heya Everybody,

I was able to figure out the answer and was able to make my 1st trial with xml a success. I was trying to work with xml and C#. So I started on with a small thing:

  1. A windows form to accept Employee Department, name, and Emp No.
  2. On the press of "Submit" button the xml will be written, then all textbox will be cleared, and xml will be displayed to the user.

Here's the working code of button click.

private void button1_Click(object sender, EventArgs e)
        {
            string path = "employee.xml";
            if (File.Exists(path))
            {
                // to append a new user having reset the textboxes
                XDocument doc = XDocument.Load(path);
                XElement xe = new XElement("user",
                    new XElement("department", textBox1.Text),
                    new XElement("name", textBox2.Text),
                    new XElement("empno", textBox3.Text)
                    );
                doc.Root.Add(xe);
                doc.Save(path);
            }
            else
            {
            new XDocument(
                new XElement("users",
                    new XElement("user",
                    new XElement("department", textBox1.Text),
                    new XElement("name", textBox2.Text),
                    new XElement("empNo", textBox3.Text)
                    )
                )
                ).Save(path);
            }
            textBox1.Text="";
            textBox2.Text="";
            textBox3.Text="";
            MessageBox.Show("Data added successfully","Done!!",MessageBoxButtons.OK);
            this.button1.Enabled=false;
            f2=new Form2();
            f2.FormClosed += Form2_FormClosed;
            f2.Show();
        }

Sadly I was not able to get any help from here(Stackoverflow) for editing the xml... so I decided to share the code with everybody here. Maybe this answer will help someone new like me.

This code could be modified with better skills and utilities, and if somebody has time & interest in helping a newbie then please do come forward.

Gud luck to all newbies here :)

gsvirdi
gsvirdi