suppose there is a class which contains 4 data fields.i have to read these value from xml file and s

Posted by SunilRai86 on Stack Overflow See other posts from Stack Overflow or by SunilRai86
Published on 2010-05-27T12:46:24Z Indexed on 2010/05/27 13:11 UTC
Read the original article Hit count: 160

Filed under:

suppose there is a class which contains 4 fields.i have to read these value from xml file and set that value to fields

the xml file is like that

<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>

now i have to read all the values from xml file and set each value to particular fields.

i hav done reading of the xml file

and i saved the retrieved value in arraylist. the code is like that

public class 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();

    }
}

here i set set the fields according to arraylist index but i dont want

is there any another solution for this....

© Stack Overflow or respective owner

Related posts about c#3.0