C# 找不到';子节点';在System.Xml.Linq.XNode中
以下代码正在.Net framwork 4.5上正确编译C# 找不到';子节点';在System.Xml.Linq.XNode中,c#,visual-studio-2012,C#,Visual Studio 2012,以下代码正在.Net framwork 4.5上正确编译 public void SetProperties(System.Xml.XmlNode properties) { int tempFor1 = properties.ChildNodes.Count; } 由于类型System.Xml.XmlNode现在在.NetCore中已过时,因此我将其替换为System.Xml.Linq.XNode。 但当我尝试在.NetCo
public void SetProperties(System.Xml.XmlNode properties)
{
int tempFor1 = properties.ChildNodes.Count;
}
由于类型System.Xml.XmlNode
现在在.NetCore
中已过时,因此我将其替换为System.Xml.Linq.XNode
。
但当我尝试在.NetCore上构建它时,它给出了“ChildNodes”未找到的错误
这是更新后的代码
public void SetProperties(System.Xml.Linq.XNode properties)
{
int tempFor1 = properties.ChildNodes.Count;
}
获取子节点数的可能解决方案是什么?如果您的XNode是元素,则将其强制转换为XElement。然后它将有一个Elements()方法,您可以使用标准Linq对对象进行计数()。如果没有看到Count()函数,请确保添加
using System.Linq;
试试这样的
public void SetProperties(System.Xml.Linq.XNode properties)
{
var element = properties as XElement;
int tempFor1 = 0;
if (element != null)
{
tempFor1 = element.Elements().Count();
}
}
没有元素属性,而是具有相同名称的函数。它没有正确的计数函数-它是一个函数。但好消息是,不带参数的函数返回完整集合,您可以对其进行计数()。