C# 从void到XmlDocument的隐式转换
也许这是个愚蠢的问题,但我对“获取并设置属性”(get and set property)这类编程还是相当陌生的 我的这部分代码不断出现编译错误C# 从void到XmlDocument的隐式转换,c#,xml,class,properties,C#,Xml,Class,Properties,也许这是个愚蠢的问题,但我对“获取并设置属性”(get and set property)这类编程还是相当陌生的 我的这部分代码不断出现编译错误 private string _File = "Session.xml"; private XmlDocument XmlDoc { get { XmlDocument _Doc = new XmlDocument(); return _Doc.LoadXml(_File); } } priv
private string _File = "Session.xml";
private XmlDocument XmlDoc
{
get
{
XmlDocument _Doc = new XmlDocument();
return _Doc.LoadXml(_File);
}
}
private XmlElement XmlRoot
{
get
{
return XmlDoc.DocumentElement;
}
}
为什么?我无法向自己解释这一点,因为我甚至没有看到任何隐式转换…\u Doc.LoadXml(\u File);返回void而不是XmlDocument。将代码更改为:
private XmlDocument XmlDoc
{
get
{
XmlDocument _Doc = new XmlDocument();
_Doc.LoadXml(_File);
return _Doc;
}
}
private XmlDocument XmlDoc
{
get
{
XmlDocument _Doc = new XmlDocument();
_Doc.LoadXml(_File);
return _Doc;
}
}
问题在于这一行:
return _Doc.LoadXml(_File);
您试图从返回类型为void
的方法返回值
请尝试以下方法:
private XmlDocument XmlDoc
{
get
{
XmlDocument _Doc = new XmlDocument();
_Doc.LoadXml(_File);
return _Doc;
}
}
此代码是您的问题:
return _Doc.LoadXml(_File);
LoadXml
方法的返回类型为void
,因为该方法不返回任何值,而是从指定的文件路径填充XmlDocument
实例
要解决问题,只需将属性更改为: