2010-05-27 37 views
0

假設有一個包含4個字段的類。必須從xml文件中讀取這些值,並且該值設置爲域假設有一個包含4個數據字段的類。我必須從xml文件讀取這些值並將該值設置爲數據字段

XML文件就是這樣

<Root> 
    <Application > 
     <AppName>somevalue</AppName> 
     <IdMark>somevalue</IdMark> 
     <ClassName>ABC</ClassName> 
     <ExecName>XYZ</ExecName> 
    </Application> 
    <Application> 
     <AppName>somevalue</AppName> 
     <IdMark>somevalue</IdMark> 
     <ClassName>abc</ClassName> 
     <ExecName>xyz</ExecName> 
    </Application> 

</Root> 

現在我要讀從XML文件中的所有值,並設置每個值特定領域。

我做甲肝讀取XML文件

和我的ArrayList中保存檢索到的值。 代碼是這樣的

公共類CXmlFileHook { string appname; string classname; string idmark; string execname; string ctor;

public CXmlFileHook() 
    { 
     this.appname = "Not Set"; 
     this.idmark = "Not Set"; 
     this.classname = "Not Set"; 
     this.execname = "Not Set"; 
     this.ctor = "CXmlFileHook()"; 

    } 
    public void readFromXmlFile(string path) 
    { 
     XmlTextReader oRreader = new XmlTextReader(@"D:\\Documents and Settings\\sunilr\\Desktop\\MLPACK.xml"); 


     //string[] strNodeValues = new string[4] { "?","?","?","?"}; 
     ArrayList oArrayList = new ArrayList(); 
     while (oRreader.Read()) 
     { 
      if (oRreader.NodeType == XmlNodeType.Element) 
      { 
       switch (oRreader.Name) 
       { 

        case "AppName": 
         oRreader.Read(); 
         //strNodeValues[0] =oRreader.Value; 
         oArrayList.Add(oRreader.Value); 
         break; 
        case "IdMark": 
         oRreader.Read(); 
         //strNodeValues[1] = oRreader.Value; 
         oArrayList.Add(oRreader.Value); 
         break; 
        case "ClassName": 
         oRreader.Read(); 
         //strNodeValues[2] = oRreader.Value; 
         oArrayList.Add(oRreader.Value); 
         break; 
        case "ExecName": 
         oRreader.Read(); 
         //strNodeValues[3] = oRreader.Value; 
         oArrayList.Add(oRreader.Value); 
         break; 
       } 
      } 
     } 
     Console.WriteLine("Reading from arraylist"); 
     Console.WriteLine("-------------------------"); 
     for (int i = 0; i < oArrayList.Count; i++) 
     { 
      //Console.WriteLine("Reading from Sting[]"+ strNodeValues[i]); 
      Console.WriteLine(oArrayList[i]); 
     } 

     //this.appname = strNodeValues[0]; 
     //this.idmark = strNodeValues[1]; 
     //this.classname = strNodeValues[2]; 
     //this.execname = strNodeValues[3]; 

     this.appname = oArrayList[0].ToString(); 
     this.idmark = oArrayList[1].ToString(); 
     this.classname = oArrayList[2].ToString(); 
     this.execname = oArrayList[3].ToString(); 






    } 
    static string vInformation; 
    public void showCurrentState(string path) 
    { 

     FileStream oFileStream = new FileStream(path, FileMode.Append, FileAccess.Write); 

     StreamWriter oStreamWriter = new StreamWriter(oFileStream); 


     oStreamWriter.WriteLine("****************************************************************"); 
     oStreamWriter.WriteLine("       Log File        "); 
     oStreamWriter.WriteLine("****************************************************************"); 

     CXmlFileHook oFilehook = new CXmlFileHook(); 
     //Type t = Type.GetType(this._classname); 
     //Type t = typeof(CConfigFileHook); 


     DateTime oToday = DateTime.Now; 
     vInformation += "Logfile created on : "; 
     vInformation += oToday + Environment.NewLine; 

     vInformation += "Public " + Environment.NewLine; 
     vInformation += "----------------------------------------------" + Environment.NewLine; 

     vInformation += "Private " + Environment.NewLine; 
     vInformation += "-----------------------------------------------" + Environment.NewLine; 
     vInformation += "ctor = " + this.ctor + Environment.NewLine; 
     vInformation += "appname = " + this.appname + Environment.NewLine; 
     vInformation += "idmark = " + this.idmark + Environment.NewLine; 
     vInformation += "classname = " + this.classname + Environment.NewLine; 

     vInformation += "execname = " + this.execname + Environment.NewLine; 
     vInformation += "------------------------------------------------" + Environment.NewLine; 
     vInformation += "Protected" + Environment.NewLine; 
     vInformation += "------------------------------------------------" + Environment.NewLine; 
     oStreamWriter.WriteLine(vInformation); 


     oStreamWriter.Flush(); 

     oStreamWriter.Close(); 

     oFileStream.Close(); 

    } 
} 

這裏我設置根據數組列表索引設置字段,但我不想

是對此有任何的另一個解決方案....

+0

你能展示你的代碼併發布問題是什麼嗎?這聽起來像你實際上閱讀過這個文件,那麼問題是什麼? – 2010-05-27 12:47:42

+1

這裏沒有問題.... – 2010-05-27 13:07:07

回答

1

由於有像Commons Digester庫爲何要寫這樣的原始代碼來實現這一點。

相關問題