Xml文件读取 [英] Xml File reading

查看:72
本文介绍了Xml文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello 

Hello 

正在编写一个应该将奇数/偶数分开并将其保存/读入xml文件的程序,但是我碰到了一个无法从中读取的问题xml并且也不显示奇数。不知道什么是错的,'真的很感激

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);
        }
    }
}







推荐答案

您好,

请提供您的XML样本,它代表您使用上述代码的所有条件,否则它是黑暗为那些寻求帮助的人提出建议。

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.


这篇关于Xml文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆