【发布时间】:2015-07-01 22:29:21
【问题描述】:
我需要知道如何从项目目录中读取 xml 文件,如下代码所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using crudpartialviewsjqureyajax.Models;
using System.Data;
using System.Xml;
namespace crudpartialviewsjqureyajax.Controllers
{
public class snehprajapatController : Controller
{
public void getinstructors()
{
try
{
List<string> name = null;
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"~\App_Data\Trainings.xml");//Here given xml path location
XmlNodeList xnList = xml.SelectNodes("/Trainings/Training");
foreach (XmlNode xn in xnList)
{
new List<string>{
xn["name"].InnerText
};
}
//return name;
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
这里出现错误是:
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: Data at the root level is invalid. Line 1, position 1.
请查看上面的代码并建议我为此做什么?
提前致谢。
【问题讨论】:
标签: c#