C# 使用XDocument检查文件中是否存在xml节
我有一些在xml文件中读取的代码。但是,它在第3条IF语句中触发错误:C# 使用XDocument检查文件中是否存在xml节,c#,xdoc,C#,Xdoc,我有一些在xml文件中读取的代码。但是,它在第3条IF语句中触发错误: if (xdoc.Root.Descendants("HOST").Descendants("Default") .FirstOrDefault().Descendants("HostID") .FirstOrDefault().Descendants("Deployment").Any()) 错误: 这是因为在此特定文件中没有[HOST]节 我假设在第一个IF语句中,如果它没有找到任何[HOST]部分,它
if (xdoc.Root.Descendants("HOST").Descendants("Default")
.FirstOrDefault().Descendants("HostID")
.FirstOrDefault().Descendants("Deployment").Any())
错误:
这是因为在此特定文件中没有[HOST]
节
我假设在第一个IF语句中,如果它没有找到任何[HOST]
部分,它将不会进入该语句,因此我不应该得到这个错误。有没有办法先检查一个部分是否存在
XDocument xdoc = XDocument.Load(myXmlFile);
if (xdoc.Root.Descendants("HOST").Any())
{
if (xdoc.Root.Descendants("HOST").Descendants("Default").Any())
{
if (xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault().Descendants("HostID").FirstOrDefault().Descendants("Deployment").Any())
{
if (xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault().Descendants("HostID").Any())
{
var hopsTempplateDeployment = xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault().Descendants("HostID").FirstOrDefault().Descendants("Deployment").FirstOrDefault();
deploymentKind = hopsTempplateDeployment.Attribute("DeploymentKind");
host = hopsTempplateDeployment.Attribute("HostName");
}
}
}
}
在该
if
块的正文中
if (xdoc.Root.Descendants("HOST").Descendants("Default").Any())
{
if (xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault().Descendants("HostID").FirstOrDefault().Descendants("Deployment").Any())
{
if (xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault().Descendants("HostID").Any())
{
var hopsTempplateDeployment = xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault().Descendants("HostID").FirstOrDefault().Descendants("Deployment").FirstOrDefault();
deploymentKind = hopsTempplateDeployment.Attribute("DeploymentKind");
host = hopsTempplateDeployment.Attribute("HostName");
}
}
}
…您已确定元素/HOST/Default
存在。但是,您不知道是否存在/HOST/Default/HostId/Deployment
。如果没有,您将得到一个NullReferenceException
,与您使用FirstOrDefault
时遇到的异常类似。通常建议在您希望元素出现的情况下,首先使用
,这将至少为您提供更好的错误消息
如果您希望元素不存在,一个简单的解决方案是沿各自的LINQ2XML轴使用?。
:
var hopsTemplateDeployment =
xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault()
?.Descendants("HostID").FirstOrDefault()
?.Descendants("Deployment").FirstOrDefault();
if (hopsTemplateDeployment != null)
{
deploymentKind = hopsTemplateDeployment.Attribute("DeploymentKind");
host = hopsTemplateDeployment.Attribute("HostName");
}
它还将为您保存嵌套的
if
子句链。有时,您需要使用Count()>0来代替Any()。如果对象为空,Any会给出一个错误。谢谢,我会试试。@jdweng:永远不要用Count()>0
代替Any()
。在长长的名单上,你们将获得可衡量的绩效影响。这也不能解决问题。
var hopsTemplateDeployment =
xdoc.Root.Descendants("HOST").Descendants("Default").FirstOrDefault()
?.Descendants("HostID").FirstOrDefault()
?.Descendants("Deployment").FirstOrDefault();
if (hopsTemplateDeployment != null)
{
deploymentKind = hopsTemplateDeployment.Attribute("DeploymentKind");
host = hopsTemplateDeployment.Attribute("HostName");
}