Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net列表中仅加载一次xml文件_C#_Xml - Fatal编程技术网

C# 在asp.net列表中仅加载一次xml文件

C# 在asp.net列表中仅加载一次xml文件,c#,xml,C#,Xml,我需要使用以下代码将XML文件加载到列表中 public class MainClass { private class listClass { public string Name; public string property1; public string property2; } private const string XMLPath = @""; private List<lis

我需要使用以下代码将XML文件加载到列表中

   public class MainClass {


    private class listClass {

        public string Name;

        public string property1;

        public string property2;


    }
    private const string XMLPath = @"";
    private List<listClass> List;

    public List<listClass> GetList() {

        if (List == null) {
            XDocument XML = XDocument.Load(XMLPath);
            List = (from _val in XML.Element("values").Elements("val")
                    select new listClass {
                        Name = _val.Element("Name= ").Value,
                        property1 = _val.Element("property1").Value,
                        property2 = _val.Element("property2").Value
                    }).ToList();

        }
        return List;
    }
}
public类MainClass{
私有类listClass{
公共字符串名称;
公共字符串属性1;
公共字符串属性2;
}
私有常量字符串XMLPath=@;
私人名单;
公共列表GetList(){
if(List==null){
XDocument XML=XDocument.Load(XMLPath);
List=(从XML.Element(“值”).Elements(“val”)中的_val)
选择新的listClass{
Name=_val.Element(“Name=).Value,
property1=值元素(“property1”).值,
property2=_val.Element(“property2”).Value
}).ToList();
}
退货清单;
}
}
我希望在整个应用程序中反复使用此列表,而不依赖于实例。XML文件将是只读的,并且只能手动更新


我是否应该将类和方法设置为静态的,这样我就不需要创建对象,也不需要从文件系统中一次又一次地加载XML来列表

是的,单例对象是最好的解决方案,我应该保持它的静态还是创建单例?如果使用单例对象,您可以进行延迟加载,如下所述:(第四或第五版)。