【发布时间】:2014-03-20 14:17:58
【问题描述】:
我正在写一个xml文件,但是当我开始写元素时,它会出现一些错误,当我尝试通过代码读取xml文件时,它找不到这些元素。
<?xml version="1.0" encoding="utf-8" ?>
<options>
<difficulty>
<type name="Easy" health="6" active="0"/>
<type name="Normal" health="4" active="1"/>
<type name="Hard" health="2" active="0"/>
</difficulty>
<soundvolume>
<type name="Sound" value="100"/>
<type name="tempSound" value="100"/>
</soundvolume>
</options>
这是目前为止的 xml 代码,但如果我不能让它工作,我不想继续。
这是我得到的错误:
找不到元素“选项”的架构信息。
并且每个元素都有相同的错误。 我使用 Visual Studio 2013 并有一个 Windows 窗体应用程序 C# 项目
这就是我读取 xml 文件的方式:
StreamReader sr = new StreamReader("Options.xml");
String xmlsr = sr.ReadToEnd();
sr.Close();
XElement xDocumentSr = XElement.Parse(xmlsr);
XElement xOptionsSr = xDocumentSr.Element("options");
XElement xDifficultySr = xOptionsSr.Element("difficulty");
foreach (XElement xType in xDifficultySr.Descendants("type"))
{
if(Convert.ToInt32(xType.Attribute("activate").Value) == 1)
{
labDifficulty.Text = xType.Attribute("name").Value;
}
}
错误发生在她:
XElement xOptionsSr = xDocumentSr.Element("options");
我得到这个错误:
Splash Screen.exe 中出现“System.NullReferenceException”类型的未处理异常
附加信息:对象引用未设置为对象的实例。
当处于调试模式时,我可以看到元素为 = null
【问题讨论】:
-
你能告诉我们你是怎么写/读的吗?
-
XML != 代码。请显示您的 C# 代码并在您的代码中显示错误发生的位置。
标签: c# xml visual-studio-2013 xelement