none
Xml File reading RRS feed

  • Question

  • Hello 

    Was writing a programm that should separate odd/even numbers and save/read it into xml file, but i bumped into a problem that it cannot read from xml and also do not show the odd numbers.Have no idea whats wrong, 'd be really appreciated

    sing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void ReadFromKeyboard()
            {
                string enter;
                Console.WriteLine("Input numbers separeted by entr");
                Console.WriteLine("After finishing push the entr button");
                amount = 0;
                do
                {
                    enter = Console.ReadLine();
                    if (int.TryParse(enter, out field[amount])) ;
                    {
                        amount++;
                    }
                } while (enter != "" && amount < size);
                if (amount == size)
                {
                    Console.WriteLine("End of field");
                }
                Console.WriteLine("Was succesfully red" + amount + "numberss");
                Console.ReadKey();
            }
    
    
            static void ReadFromFile()
            {
                string Name;
                Console.WriteLine("Input name of file");
                Name = Console.ReadLine();
                if (!File.Exists(Name))
                {
                    Console.WriteLine("No matches");
    
                }
                else
                {
                    using (StreamReader download = new StreamReader(Name))
                    {
                        amount = 5;
                        while (download.EndOfStream && amount < size)
                        {
                            if (int.TryParse(download.ReadLine(), out field[amount]))
                            {
                                amount++;
                            }
                        }
                       
                    }
                }
                Console.ReadKey();
            }
    
            static bool CanRewrite(string Name)
            {
                if (File.Exists(Name))
                {
                    Console.WriteLine("File's already exist rewrite ?");
                    char choise = char.ToLower(Console.ReadKey().KeyChar);
                    if (choise == 'a') return true;
                    else return false;
    
                }
                else return true;
            }
    
            static void SaveittoFile()
            {
                string name;
                Console.WriteLine("Input a name of file");
                name = Console.ReadLine();
                if (CanRewrite(name))
                {
                    using (StreamWriter write = new StreamWriter(name))
                    {
                        for (int i = 0; i < amount; i++)
                        {
                            write.WriteLine(field[i]);
                        }
                        Console.WriteLine("Successfully written" + amount + "numbers" + name);
                        Console.ReadKey();
                    }
                }
            }
            
            static void WriteNum(char wich)
            {
                for (int i = 0; i < amount; i++)
                {
                    if (wich == 'a') Console.WriteLine(field[i]);
                    if (wich == 'e' && (field[i] % 2 == 0)) Console.WriteLine(field[i]);
                    if (wich == 'o' && (field[i] % 2 == 1)) Console.WriteLine(field[i]);
                }
                Console.ReadKey();
            }
    
            const int size = 1000;
            static int[] field;
            static int amount;
    
            static void Main(string[] args)
            {
                field = new int[size];
                amount = 0;
                int choise;
                do
                {
                    Console.Clear();
                    Console.WriteLine("1 Read from a keyboard");
                    Console.WriteLine("2 Read a File");
                    Console.WriteLine("3 Write into a file");
                    Console.WriteLine("4 Show numbers");
                    Console.WriteLine("5 Find odd or even numbers");
                    Console.WriteLine("6 End");
    
    
    
                    int.TryParse(Console.ReadLine(), out choise);
                    switch (choise)
                    {
                        case 1:
                            ReadFromKeyboard();
                            break;
                        case 2:
                            ReadFromFile();
                            break;
                        case 3:
                            SaveittoFile();
                            break;
                        case 4:
                            WriteNum('v');
                            for (int i = 0; i < amount; i++)
                            {
                                Console.WriteLine(field[i]);
                            }
                            Console.ReadKey();
                            break;
                        case 5:
                            Console.WriteLine("Choose odd or even numbers [e or o]");
                            char choice2 = char.ToLower(Console.ReadKey().KeyChar);
                            Console.WriteLine();
                            if (choice2 == 'e' || choise == 'o')
                            {
                                WriteNum(choice2);
                                Console.ReadKey();
                            }
                            else
                            {
                                Console.WriteLine("Wrong input");
                            }
                            break;
                        case 6:
                            Console.WriteLine("Bye");
                            break;
                        default:
                            Console.WriteLine("Wrong input");
                            break;
                    }
    
                }
                while (choise != 6);
            }
        }
    }
    


    • Edited by Useeeer Wednesday, April 11, 2018 10:47 AM Code wasnt written correct
    Wednesday, April 11, 2018 10:37 AM

All replies

  • Hello,

    Please provide a sample of your XML which represents all conditions you are using the above code for, otherwise its a shot in the dark making a recommendation for those who are looking to assist with this.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, April 11, 2018 1:47 PM
    Moderator
  • Your read logic is backwards.

    //Incorrect - while at the end of the file and amount is less than size (1000)
    while (download.EndOfStream && amount < size)
                        {
                            if (int.TryParse(download.ReadLine(), out field[amount]))
                            {
                                amount++;
                            }
                        }
    
    //Correct - read until we run out of space in our array
    int index = 0;
    while (!download.EndOfStream && index < field.Length)
    {
       string line = download.ReadLine();
    
       //Ignoring non-ints??
       if (int.TryParse(line, out int value))
          field[index++] = value;
    };


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 11, 2018 1:58 PM
    Moderator