字段元素字典查找

时间:2015-10-26 17:16:06

标签: c# arrays dictionary

提前为一个愚蠢的问题道歉。我从3天前的C#开始。我需要自动化文档创建。文档是从配置输出创建的,我将其转换为xml,然后转换为word。到目前为止,我已经设法结合了XMLwriter和FieldParser的魔力,如下所示。

我现在想添加一个排序查找。例如,int _librarytype;下面会返回一个例如" 8"我想打印相关的值" TLD"。我读过关于字典的内容,但不能读到字典。弄清楚如何或在何处将它们应用于数组成员。

任何指导都将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Xml;

class Program
{
    class StorageUnit
    {
        string _storagename;
        int _storageunittype;
        string _hostConnection;
        int _librarytype;
        int _robotnumber;
        int _density;
        int _maxconjobsnumberdrives;
        int _multiplexing;
        int _fragsize;
        string _diskpool;

        public StorageUnit(string storageunitname, int storageunittype, string hostconnection, int librarytype, int robotnumber, int density, int maxconjobsnumberdrives, int multiplexing, int fragsize, string diskpool)
        {
            _storagename = storageunitname;
            _storageunittype = storageunittype;
            _hostConnection = hostconnection;
            _librarytype = librarytype;
            _robotnumber = robotnumber;
            _density = density;
            _maxconjobsnumberdrives = maxconjobsnumberdrives;
            _multiplexing = multiplexing;
            _fragsize = fragsize;
            _diskpool = diskpool;
        }

        public string storageunitname { get { return _storagename; } }
        public int storageunittype { get { return _storageunittype; } }
        public string hostconnection { get { return _hostConnection; } }
        public int librarytype { get { return _librarytype; } }
        public int robotNumber { get { return _robotnumber; } }
        public int density { get { return _density; } }
        public int maxconjobsnumberdrives { get { return _maxconjobsnumberdrives; } }
        public int multiplexing { get { return _multiplexing; } }
        public int fragsize { get { return _fragsize; } }
        public string diskpool { get { return _diskpool; } }
    }

    static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = "\t";
        using (TextFieldParser storageunits = new TextFieldParser("C:\\Input Files\\bpstulist.txt"))
        using (XmlWriter writer = XmlWriter.Create("C:\\Output Files\\StorageUnits.xml",settings))
        {
            storageunits.TextFieldType = FieldType.Delimited;
            storageunits.Delimiters = new string[] { " " };
            storageunits.HasFieldsEnclosedInQuotes = true;
            storageunits.TrimWhiteSpace = true;


            string[] stuprops;
            int stunumber = 0;
            writer.WriteStartDocument();
            writer.WriteStartElement("StorageUnits");
            while (!storageunits.EndOfData)
            {
                stuprops = storageunits.ReadFields();
                writer.WriteStartElement("StorageUnit");
                writer.WriteElementString("StorageUnitName", stuprops[0]);
                writer.WriteElementString("StorageUnitType", stuprops[1].ToString());
                writer.WriteElementString("HostConnection", stuprops[2]);
                writer.WriteElementString("LibraryType", stuprops[3].ToString());
                writer.WriteElementString("RobotNumber", stuprops[4].ToString());
                writer.WriteElementString("Density", stuprops[5].ToString());
                writer.WriteElementString("MaxConcurrentDrives", stuprops[6].ToString());
                writer.WriteElementString("Multiplexing", stuprops[10].ToString());
                writer.WriteElementString("FragmentSize", stuprops[11].ToString());
                writer.WriteElementString("Diskpool", stuprops[19]);
                writer.WriteEndElement();
                stunumber++;
                     writer.WriteEndElement();
            writer.WriteEndDocument();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我明白了。我添加了

Dictionary<int, string> lib_lookup = new Dictionary<int, string>()
                                                            {
                                                                {0, "NONE"},
                                                                {1, "ACS"},
                                                                {2, "TS8"},
                                                                {5, "ODL"},
                                                                {6, "TL8"},
                                                                {7, "TL4"},
                                                                {8, "TLD"},
                                                                {10, "TSD"},
                                                                {11, "TSH"},
                                                                {12, "TLH"},
                                                                {13, "TLM"},
                                                                {17, "LMF"},
                                                             }; 

然后我稍后提到它以获得正确的值。

                        int librarynumber;
                    bool libparsed = Int32.TryParse((stuprops[3]), out librarynumber);
                    string libtypestring = lib_lookup[(librarynumber)];
                    writer.WriteElementString("LibraryType", libtypestring);

字符串到int的字符串看起来有点复杂,但我还在学习。