Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
如何优化下面的代码,要求是在对象初始化事件上从xml读取配置_Xml_Linq - Fatal编程技术网

如何优化下面的代码,要求是在对象初始化事件上从xml读取配置

如何优化下面的代码,要求是在对象初始化事件上从xml读取配置,xml,linq,Xml,Linq,如何优化下面的代码,要求是在对象初始化事件上从xml读取配置 try { var XMLInput = (XDocument.Load(cmdParameters["Profile"].ToString()).Descendants("Profile") .Select(profile => new Profile { Server = profile.Element("Server").Value,

如何优化下面的代码,要求是在对象初始化事件上从xml读取配置

try
{
    var XMLInput = (XDocument.Load(cmdParameters["Profile"].ToString()).Descendants("Profile")
        .Select(profile => new Profile
        {
            Server = profile.Element("Server").Value,
            Publication = Convert.ToInt32(profile.Element("Publication").Value)
        })).Single();

    this.ServerName = XMLInput.Server;
    this.NumberOfPublications = XMLInput.Publication;                        
}
catch (Exception)
{
    Logger.Log(string.Format("Unable to load the Profile : {0}", cmdParameters["Profile"].ToString()));
}

如何将值直接加载到当前对象中(此),而不是创建临时对象并加载。

您不必创建新的配置文件对象。您可以直接从
XElement
对象获取属性值,如下所示:

var-xElement=(XDocument.Load(cmdParameters[“Profile”].ToString())
.Profile.Single();
this.ServerName=xElement.Element(“服务器”).Value;
this.NumberOfPublications=Convert.ToInt32(xElement.Element(“Publication”).Value);

请注意,此代码假定xElement对象永远不会为null。如果不是这样,您可能希望将
Single()
替换为
SingleOrDefault()
,并在分配值之前检查对象是否为空。

您不必创建新的配置文件对象。您可以直接从
XElement
对象获取属性值,如下所示:

var-xElement=(XDocument.Load(cmdParameters[“Profile”].ToString())
.Profile.Single();
this.ServerName=xElement.Element(“服务器”).Value;
this.NumberOfPublications=Convert.ToInt32(xElement.Element(“Publication”).Value);

请注意,此代码假定xElement对象永远不会为null。如果不是这样,您可能希望将
Single()
替换为
SingleOrDefault()
,并在分配值之前检查对象是否为空。

您不必创建新的配置文件对象。您可以直接从
XElement
对象获取属性值,如下所示:

var-xElement=(XDocument.Load(cmdParameters[“Profile”].ToString())
.Profile.Single();
this.ServerName=xElement.Element(“服务器”).Value;
this.NumberOfPublications=Convert.ToInt32(xElement.Element(“Publication”).Value);

请注意,此代码假定xElement对象永远不会为null。如果不是这样,您可能希望将
Single()
替换为
SingleOrDefault()
,并在分配值之前检查对象是否为空。

您不必创建新的配置文件对象。您可以直接从
XElement
对象获取属性值,如下所示:

var-xElement=(XDocument.Load(cmdParameters[“Profile”].ToString())
.Profile.Single();
this.ServerName=xElement.Element(“服务器”).Value;
this.NumberOfPublications=Convert.ToInt32(xElement.Element(“Publication”).Value);

请注意,此代码假定xElement对象永远不会为null。如果不是这样,您可能希望将
Single()
替换为
SingleOrDefault()
,并在分配值之前检查对象是否为空。

您不必创建临时的
配置文件
对象,并直接从
XElement
获取所需的值,另一个答案中也解释了这一点。除此之外,您可以直接将
XElement
强制转换为
int
-或某些其他类型*,以获取
int
类型中的元素值:

var profile=XDocument.Load(cmdParameters[“profile”].ToString())
.后代(“简介”)
.Single();
this.ServerName=(string)profile.Element(“服务器”);
this.NumberOfPublications=(int)profile.Element(“出版物”);

*:针对
XElement
的可能显式转换列表:

您不必创建临时
Profile
对象,并直接从
XElement
获取所需值,另一个答案中也解释了这一点。除此之外,您可以将
XElement
强制转换为
int
-或其他类型*-直接获取
int
类型中的元素值:

var profile=XDocument.Load(cmdParameters[“profile”].ToString())
.后代(“简介”)
.Single();
this.ServerName=(string)profile.Element(“服务器”);
this.NumberOfPublications=(int)profile.Element(“出版物”);

*:针对
XElement
的可能显式转换列表:

您不必创建临时
Profile
对象,并直接从
XElement
获取所需值,另一个答案中也解释了这一点。除此之外,您可以将
XElement
强制转换为
int
-或其他类型*-直接获取
int
类型中的元素值:

var profile=XDocument.Load(cmdParameters[“profile”].ToString())
.后代(“简介”)
.Single();
this.ServerName=(string)profile.Element(“服务器”);
this.NumberOfPublications=(int)profile.Element(“出版物”);

*:针对
XElement
的可能显式转换列表:

您不必创建临时
Profile
对象,并直接从
XElement
获取所需值,另一个答案中也解释了这一点。除此之外,您可以将
XElement
强制转换为
int
-或其他类型*-直接获取
int
类型中的元素值:

var profile=XDocument.Load(cmdParameters[“profile”].ToString())
.后代(“简介”)
.Single();
this.ServerName=(string)profile.Element(“服务器”);
this.NumberOfPublications=(int)profile.Element(“出版物”);

*:XElement的可能显式转换列表

当前对象的类型是什么?是“配置文件”吗?不是,它是一种配置类型。只是创建了配置文件类以临时加载数据。当前对象的类型是什么?是“配置文件”?不,这是一种配置。只是创建了概要文件类以临时加载数据。W