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

C# 3.0 假设有一个类包含4个数据字段,我必须从xml文件中读取这些值,并将该值设置为数据字段,c#-3.0,C# 3.0,假设有一个类包含4个字段,我必须从xml文件中读取这些值,并将该值设置为字段 xml文件是这样的 <Root> <Application > <AppName>somevalue</AppName> <IdMark>somevalue</IdMark> <ClassName>ABC</ClassName> <ExecName

假设有一个类包含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>

一些价值
一些价值
基础知识
XYZ
一些价值
一些价值
abc
xyz
现在我必须从xml文件中读取所有值,并将每个值设置为特定字段

我已经完成了xml文件的读取

我将检索到的值保存在arraylist中。 代码是这样的

<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>
公共类CXmlFileHook { 字符串appname; 字符串类名称; 字符串标识; 字符串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();

    }
}
public CXmlFileHook()
{
this.appname=“未设置”;
this.idmark=“未设置”;
this.classname=“未设置”;
this.execname=“未设置”;
this.ctor=“CXmlFileHook()”;
}
public void readFromXmlFile(字符串路径)
{
XmlTextReader或Reader=新的XmlTextReader(@“D:\\Documents and Settings\\sunilr\\Desktop\\MLPACK.xml”);
//string[]strNodeValues=新字符串[4]{“?”,“?”,“?”,“?”,“?”};
ArrayList oArrayList=新的ArrayList();
while(或reader.Read())
{
if(oRreader.NodeType==XmlNodeType.Element)
{
开关(或读卡器名称)
{
案例“AppName”:
或reader.Read();
//strNodeValues[0]=或reader.Value;
oArrayList.Add(或reader.Value);
打破
案例“IdMark”:
或reader.Read();
//strNodeValues[1]=或读取值;
oArrayList.Add(或reader.Value);
打破
案例“ClassName”:
或reader.Read();
//strNodeValues[2]=或读取值;
oArrayList.Add(或reader.Value);
打破
案例“ExecName”:
或reader.Read();
//strNodeValues[3]=或读取值;
oArrayList.Add(或reader.Value);
打破
}
}
}
Console.WriteLine(“从arraylist读取”);
Console.WriteLine(“---------------------------”);
for(int i=0;i
这里我根据arraylist索引设置字段,但我不想


有没有其他解决方案……

因为有这样的库,为什么要编写这样的原始代码来实现这一点。

你能展示一下你的代码,然后发布问题是什么吗?听起来你好像真的读过这个文件,那么有什么问题吗?这里没有问题。。。。