[英]C# ASP.NET: 'Object reference not set to an instance of an object.' System.Xml.Linq.XElement.Attribute(…) returned null
Creating an application for getting,updating,deleting movies创建用于获取、更新、删除电影的应用程序
I'm attempting to get my movies list from an XML file but everytime I go on the WebService tester and try getting the list of movies I get hit with this error我正在尝试从 XML 文件中获取我的电影列表,但每次我在 WebService 测试仪上使用 go 并尝试获取我遇到此错误的电影列表
'Object reference not set to an instance of an object.'
“对象引用未设置为 object 的实例。” System.Xml.Linq.XElement.Attribute(...) returned null
System.Xml.Linq.XElement.Attribute(...) 返回 null
My code我的代码
public class Movie
{
private static string _xmlDataPath = HttpContext.Current.Server.MapPath("~/App_Data/Movies.xml");
public int Id { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public string Synopsis { get; set; }
public Movie(int id, string title, string director, string sypnosis)
{
this.Id = id;
this.Title = title;
this.Director = director;
this.Synopsis = sypnosis;
}
public XElement toXML(XNamespace ns)
{
return new XElement(ns + "Movie",
new XElement(ns + "Id", this.Id),
new XElement(ns + "Title", this.Title),
new XElement(ns + "Director", this.Director),
new XElement(ns + "Sypnosis", this.Synopsis)
);
}
public static List<Movie> GetAllMovies()
{
List<Movie> movieList = new List<Movie>();
//Load XML file using LINQ
XDocument doc = XDocument.Load(_xmlDataPath);
var xmlMoviesList = doc.Root.Elements();
XNamespace ns = doc.Root.GetDefaultNamespace();
//Get each movie element from XML
foreach (var xmlMovies in xmlMoviesList)
{
Movie movie = new Movie(
int.Parse(xmlMovies.Attribute("Id").Value),
xmlMovies.Element(ns + "Title").Value,
xmlMovies.Element(ns + "Director").Value,
xmlMovies.Element(ns + "Sypnosis").Value
);
//Add to the list
movieList.Add(movie);
}
//Return the list
return movieList;
}
It would be great if you could include sample XML, otherwise people have to take a guess.如果您可以包含样品 XML,那就太好了,否则人们不得不猜测。
Your code works fine parsing this XML file, with Id
as an attribute.您的代码可以很好地解析此 XML 文件,并将
Id
作为属性。
<Root>
<Movie Id="1">
<Title>Alien</Title>
<Director>Ridley Scott</Director>
<Sypnosis>In space no one can hear you scream.</Sypnosis>
</Movie>
</Root>
However the toXML
function you have written produces this XML which has Id
as a node但是,您编写的
toXML
function 会生成此 XML ,其Id
为节点
<Root>
<Movie>
<Id>1</Id>
<Title>Alien</Title>
<Director>Ridley Scott</Director>
<Sypnosis>In space no one can hear you scream.</Sypnosis>
</Movie>
</Root>
Decide if you want the Id
to be an attribute or a node, and adjust the code accordingly.确定您希望
Id
是属性还是节点,并相应地调整代码。 Either change new XElement(ns + "Id", this.Id),
to new XAttribute(ns + "Id", this.Id)
, or change xmlMovies.Attribute("Id").Value
to xmlMovies.Element(ns + "Id").Value
要么将
new XElement(ns + "Id", this.Id),
更改为new XAttribute(ns + "Id", this.Id)
,要么将xmlMovies.Attribute("Id").Value
更改为xmlMovies.Element(ns + "Id").Value
On a side note, have a look at the documentation for int.TryParse
, and try to use it in place of int.Parse
.附带说明一下,查看
int.TryParse
的文档,并尝试使用它来代替int.Parse
。 Best to make sure that you first have an integer in the data being parsed最好确保您首先在正在解析的数据中有一个 integer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.