2015-10-26 64 views
0

爲了一個愚蠢的問題提前道歉。我從3天前開始使用C#。我有一個要求自動創建文檔。這些文檔是從配置輸出創建的,我將其轉換爲xml,然後轉換爲單詞。到目前爲止,我已經設法將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(); 
     } 
    } 
} 
+0

「TLD」從哪裏來? – PaulF

+0

TLD是一種磁帶庫類型,它的已知但未在配置輸出中使用。在SQL中,我將執行查找來執行此功能。 「C:\\ Input Files \\ bpstulist.txt」中的庫類型被定義爲一個數字(1,2,3,4,5,6等)。其中每一個都指特定的庫(磁帶庫)類型(ASC,TLD,TLH等)。我知道這些是什麼,並希望將它們記錄在某處,以便在輸出中顯示字符串而不是整數。 –

+0

還有一件事,我倒過來設計了一個例子來達到上面的狀態。我不知道類StorageUnit提供什麼。如果不存在,我仍然可以從我的文件中獲取信息並將其寫入xml。它是多餘的嗎? –

回答

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到字符串似乎有點複雜,但我仍然在學習。