字典类和Web服务 [英] Dictionary Class and Web Services

查看:67
本文介绍了字典类和Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在寻求帮助,但我完全陷入困境。我正在尝试创建一个字典类,它从存储在我的硬盘驱动器上的CSV文件中填充,在WebService中工作,所以我可以输入一个单词,意思是输出(所有这些都在CSV文件中)可以有人帮忙吗?我一直在网上看,但到目前为止我找不到任何对我有用的东西。而我正试图在网络服务中这样做。



这是我的代码;



到目前为止我有这个;



[WebMethod]

字符串行;

StreamReader reader = new StreamReader(myfile.txt); //写入文件

[WebMethod]

公共类

{

private Dictionary< string,> _regionTimeValues = new Dictionary< string,>();

private String _words;





}



我知道这不是很多但是在我弄清楚如何使用CSV文件作为字典来填充web服务之后我应该很高兴。



谢谢。

Hi,

I'm looking for some help but I'm completely stuck. I'm trying to create a Dictionary Class that gets populated from a CSV file stored on my hard drive an works within a WebService, so I can input a word and the meaning is the output(all of which is in the CSV file) Can anyone help with that? I've been looking online but I can't find anything that's worked for me so far. And I'm trying to do this in a webservice.

This is the code I have;

So far I have this;

[WebMethod]
string line;
StreamReader reader = new StreamReader("myfile.txt"); //writes to file
[WebMethod]
public class
{
private Dictionary<string,> _regionTimeValues = new Dictionary<string,>();
private String _words;


}

I know it's not a lot but after I figure out how to populate the webservice from using the CSV file as a dictionary I should be good to go.

Thank You.

推荐答案

您好,



看看这个可运行的例子。随时提出任何问题...



Hi,

have a look at this runnable example. feel free to ask any questions...

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ReadCSVToDictionary
{
    class Program
    {
        // Wrapper class to represent the information in one line of the csv
        class LineInformation
        {
            public string Key { get; set; }
            public string StringValue { get; set; }
            public int IntValue { get; set; }
            public DateTime DateTimeValue { get; set; }
        }

        static void Main(string[] args)
        {
            // Replace with path to your data file
            string strPathToCSVFile = @"myfile.csv";

            #region Create example file

            List<lineinformation> listExample = new List<lineinformation>();
            listExample.Add(new LineInformation { Key = "value1", StringValue = "blabla", IntValue = 1, DateTimeValue = DateTime.Now });
            listExample.Add(new LineInformation { Key = "value2", StringValue = "ahahah", IntValue = 44, DateTimeValue = DateTime.Now.AddHours(1) });
            listExample.Add(new LineInformation { Key = "value3", StringValue = "what?", IntValue = 5519, DateTimeValue = DateTime.Now.AddDays(1) });

            StringBuilder sbFileContent = new StringBuilder();
            foreach (LineInformation li in listExample)
            {
                sbFileContent.AppendFormat("{0};{1};{2};{3}" + Environment.NewLine, li.Key, li.StringValue, li.IntValue, li.DateTimeValue);
            }
            File.WriteAllText(strPathToCSVFile, sbFileContent.ToString());

            #endregion // Create example file

            // Read all lines from file
            string[] astrLines = File.ReadAllLines(strPathToCSVFile);

            // Go through all lines
            const char chSeparator = ';';
            Dictionary<string,> dict = new Dictionary<string,>();
            foreach (string strLine in astrLines)
            {
                // Split the lines by the used separator sign (comma, tab, ...)
                string[] astrSplit = strLine.Split(chSeparator);

                // create and intialize an instance of the type representing the information
                // from the csv file - in our example a LineInformation object.
                // In real you have to know how the csv columns are ordered and formatted (culture specific?)
                LineInformation lineinformation = new LineInformation
                {
                    Key = astrSplit[0], // we assume first column of csv holds the key
                    StringValue = astrSplit[1], // ... second column is our string value
                    IntValue = Convert.ToInt32(astrSplit[2]), // ... third column is an integer - we also convert it's value
                    DateTimeValue = DateTime.Parse(astrSplit[3])
                };

                // Add the instance to our Dictionary
                dict.Add(lineinformation.Key, lineinformation);
            }

            // use your dictionary here
            foreach(KeyValuePair<string,> kvp in dict)
            {
                Console.WriteLine("{0};{1};{2};{3}", kvp.Value.Key, kvp.Value.StringValue, kvp.Value.IntValue, kvp.Value.DateTimeValue);
            }

            Console.ReadKey();
        }


    }
}


这篇关于字典类和Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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