代码之家  ›  专栏  ›  技术社区  ›  ttarchala

假设有一个类包含4个数据字段,我必须从XML文件中读取这些值,并将该值设置为数据字段

  •  0
  • ttarchala  · 技术社区  · 6 年前

    假设有一个类包含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 { 字符串名称; 字符串类名; 字符串IDMARK; 字符串execname; 字符串选择器;

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

    在这里,我根据arraylist索引设置字段,但我不想

    还有其他解决办法吗?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Arun P Johny    15 年前

    因为有这样的图书馆 Commons Digester 为什么要编写这样的原始代码来实现这一点呢?