2 Replies - 2507 Views - Last Post: 29 January 2013 - 11:32 AM Rate Topic: -----

#1 sumanthan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 12

How to convert the txt file in XML

Posted 29 January 2013 - 10:44 AM

I need to convert the .txt file in to .Xml file using c#, I can only able to convert half of the text, But I needed to convert in to a readable xml file containing its roots ant its each child elements.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Creating_Text_File_Using_Xml
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlTextWriter XTW = new XmlTextWriter("savefile.xml", Encoding.Unicode);
            XTW.WriteStartDocument();
            XTW.WriteStartElement("XMLFILE");
            XTW.WriteStartElement("Title");
            XTW.WriteString(textBox1.Text);
            XTW.WriteEndElement();
            foreach(String item in listBox1.Items)
            {
                 XTW.WriteStartElement("Item");
                 XTW.WriteString(item);
                 XTW.WriteEndElement();
            }
            XTW.WriteEndElement();
            XTW.WriteEndDocument();
            XTW.Close();

        }
    }
}


This is the file i am trying to convert!!!!
http://www.2shared.c...V1611Bible.html
These are the words should be excluded from the file
http://www.2shared.c...x9/exclude.html

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert the txt file in XML

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,282
  • Joined: 05-May 12

Re: How to convert the txt file in XML

Posted 29 January 2013 - 11:14 AM

An example of your target output would probably be very instructive because the code you posted doesn't even open a text file to show what you are trying to convert.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,929
  • Joined: 02-June 10

Re: How to convert the txt file in XML

Posted 29 January 2013 - 11:32 AM

Personally I would read the text file and make objects out of it.
Then serialize the objects to XML. But I certainly wouldn't be micromanaging the writing process as you have.

Considering how little we know of what you are reading and what you want for output I doubt there is anything more specific we can offer as advice.

[*]Separating data from GUI - PLUS - serializing the data to XML

You should also give your controls meaningful names right off. Because this Button1_click stuff is horrid for long term understanding and maintainability.
Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: dnt no wut i m do-n, coz, al gud, b4, ny1, sum1, u r, and so on like this guy. Its completely disrespectful to the senior coding professionals that volunteer here to mentor you.

    Spoiler

[/spoiler]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1