Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

C# rad sa tekstualnim fajlovima

[es] :: .NET :: C# rad sa tekstualnim fajlovima

[ Pregleda: 3542 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

GRocky
Rodic Vladimir
Novi Sad

Član broj: 77687
Poruke: 54



+1 Profil

icon C# rad sa tekstualnim fajlovima18.02.2010. u 15:26 - pre 171 meseci
Zdravo svima,

zvanicno se ukljucujem u rad .NET foruma :) sa dva pitanja.

Imam jedan txt fajl sa imenima gradova cities.txt u kom su redovi tipa:

2334;Vösendorf;AUT

i od tog txt fajla trebam da napravim cities.xml fajl tipa:

<Ort>
<PLZ>2334</PLZ>
<Ort>Vö�sendorf</Ort>
<Staat>AUT</Staat>
</Ort>

2 problema mi se javljaju, pa bih zeleo ako neka zna da mi odgovori zbog cega i kako da ih resim.

1. Prvi problem je da mi u konkretnom gornjem primeru izbaci za grad Vsendorf umesto Vö�sendorf dakle fali ö�. Pretpostavljam zato sto je umlaut a ne standardan znak. Xml dokument sam ovako definisao: <?xml version="1.0" encoding="utf-8" ?>

2. Drugi problem je da mi u xml fajl program nije upisao sve gradove. U jednom momentu je samo stao sa upisivanjem. Upisao je 232253 redova, a treba da ih bude 253345.

U nastavku vam saljem primer koda koji sam koristio.

Code:

        static void Main(string[] args)
        {
            string line;
            // Read the file and display it line by line.
            System.IO.StreamReader file =
                new System.IO.StreamReader(@"C:\Users\Vladimir\Documents\Visual Studio 2008\Projects\txtToXml\txtToXml\cities.txt");
            System.IO.StreamWriter inFile = new System.IO.StreamWriter(@"C:\Users\Vladimir\Documents\Visual Studio 2008\Projects\txtToXml\txtToXml\cities.xml", true);
            while ((line = file.ReadLine()) != null)
            {
                string[] parts = line.Split(';');
                inFile.WriteLine("<Ort>");
                inFile.WriteLine("    <PLZ>" + parts[0] + "</PLZ>");//PLZ
                inFile.WriteLine("    <Ort>" + parts[1] + "</Ort>");//Ort
                inFile.WriteLine("    <Staat>" + parts[2] + "</Staat>");//Staat
                inFile.WriteLine("</Ort>");
            }

            file.Close();
            // Suspend the screen.
            System.Console.Write("GOTOVO");
            System.Console.ReadLine();

        }


Hvala svima unapred na idejama i eventualnoj pomoci !
Before you criticize someone, walk a mile in their shoes.
By the time they get angry you're a mile away and you've got their shoes!
 
Odgovor na temu

malo_nj
novi sad

Član broj: 140825
Poruke: 104
*.dynamic.sbb.rs.

Sajt: www.2advanced.com


Profil

icon Re: C# rad sa tekstualnim fajlovima18.02.2010. u 16:37 - pre 171 meseci
eve ga ovaj orts sam stavio jer se buni ako nema jedan korijeni element. ne moras ti ovako mozes isto sa
System.IO.StreamWriter inFile kako si i dosad samo trebas postaviti dobar encoding. Kod mene radi default pa probaj. A to sto ti ne prebaci sve gradove ne znam. posalji mi taj svoj txt file pa cu pogledati
Code:

 string line;
            // Read the file and display it line by line.
            // create reader & open file
            TextReader tr = new StreamReader("c://s.txt", Encoding.Default);

            // read a line of text

            XmlTextWriter xmlW = new XmlTextWriter("c:\\s2.xml", Encoding.Default);
            xmlW.WriteStartDocument();
            // close the stream
            xmlW.WriteStartElement("Orts");
            while ((line = tr.ReadLine()) != null)
            {
                string[] parts = line.Split(';');

                xmlW.WriteStartElement("Ort"); xmlW.WriteString("\n\t");
                    xmlW.WriteStartElement("PLZ"); 
                        xmlW.WriteString(parts[0]);
                    xmlW.WriteEndElement(); xmlW.WriteString("\n\t");
                    xmlW.WriteStartElement("Ort2");
                        xmlW.WriteString(parts[1]);
                    xmlW.WriteEndElement(); xmlW.WriteString("\n\t");
                    xmlW.WriteStartElement("Staat");
                        xmlW.WriteString(parts[2]);
                    xmlW.WriteEndElement();
                xmlW.WriteEndElement(); xmlW.WriteString("\n");
               // break;
            }
            xmlW.WriteEndElement();
            xmlW.WriteEndDocument();
            xmlW.Close();
            tr.Close();
            // Suspend the screen.
            System.Console.Write("GOTOVO");
          //  System.Console.ReadLine();
 
Odgovor na temu

ravni

Član broj: 8894
Poruke: 373



+15 Profil

icon Re: C# rad sa tekstualnim fajlovima18.02.2010. u 20:51 - pre 171 meseci
sto se tice problema pod 2. - tu ti najverovatnije fali neki inFile.Flush() pre close-a
 
Odgovor na temu

sallle
Sasa Ninkovic
GTECH
Beograd

Član broj: 146
Poruke: 480
91.148.88.*

ICQ: 20785904


+4 Profil

icon Re: C# rad sa tekstualnim fajlovima18.02.2010. u 22:18 - pre 171 meseci
mozda imas prazan red u ulaznom fajlu , ili red ciji line.split ne vraca tri elementa, pa ti puca program (stavi sve pod try/catch, pa vidi sta se desava)

za streamreader i streamwriter podesi enkodinge npr:

StreamReader sr = new StreamReader ("path",Encoding.UTF8);


@ravni mislim da se flush() implicitno poziva prilikom close()
 
Odgovor na temu

malo_nj
novi sad

Član broj: 140825
Poruke: 104
*.dynamic.sbb.rs.

Sajt: www.2advanced.com


Profil

icon Re: C# rad sa tekstualnim fajlovima19.02.2010. u 13:37 - pre 171 meseci
UTF-8 je barem meni vracao kockice (il upitnike zaboravih sad). A za drugi problem pa najvjerovatnije ima neku gresku u tom text file-u
 
Odgovor na temu

GRocky
Rodic Vladimir
Novi Sad

Član broj: 77687
Poruke: 54



+1 Profil

icon Re: C# rad sa tekstualnim fajlovima20.02.2010. u 16:53 - pre 171 meseci
Problem je resen.

hvala vam svima na pomoci !
Before you criticize someone, walk a mile in their shoes.
By the time they get angry you're a mile away and you've got their shoes!
 
Odgovor na temu

sstanko78
Novi Sad

Član broj: 19139
Poruke: 411
*.dynamic.sbb.rs.



Profil

icon Re: C# rad sa tekstualnim fajlovima22.02.2010. u 22:19 - pre 171 meseci
Za sledeći put:

http://filehelpers.sourceforge.net/

You can strong type your flat file (fixed or delimited) simply describing a class that maps to each record and later read/write your file as an strong typed .NET array.

Odličan free lib.
 
Odgovor na temu

[es] :: .NET :: C# rad sa tekstualnim fajlovima

[ Pregleda: 3542 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.