简体   繁体   English

C# ASP.NET:“对象引用未设置为 object 的实例。” System.Xml.Linq.XElement.Attribute(...) 返回 null

[英]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.

相关问题 C#递归方法返回空引用,对象引用未设置为对象的实例。 XElement对象 - C# Recursive method returning null reference, object reference not set to an instance of an object. XElement Objects C# Object 引用未设置为 object 的实例。 ASP.NET - C# Object reference not set to an instance of an object. ASP.NET “对象引用未设置为对象的实例。”嵌套列表ASP.NET MVC LINQ实体框架 - “Object reference not set to an instance of an object.” Nesting Lists ASP.NET MVC LINQ Entity Framework &#39;你调用的对象是空的。&#39; 在C#linq to SQL中 - 'Object reference not set to an instance of an object.' in C# linq to SQL 对象引用未设置为对象asp.net的实例,c# - Object reference not set to an instance of an object asp.net , c# Gettting对象引用未设置为对象的实例。 使用LINQ C#数据表angularjs? - Gettting Object reference not set to an instance of an object. using linq c# datatable angularjs? 在asp.net中更改页面时,为什么会收到“对象引用未设置为对象实例”的信息? - Why do I get “Object reference not set to an instance of an object.” when changing pages in asp.net C#中的LINQ DataContext.SubmitChanges()报告“对象引用未设置为对象的实例”。 - LINQ DataContext.SubmitChanges() in C# Reporting “Object reference not set to an instance of an object.” ASP.NET MVC“'对象引用未设置为 object 的实例。'”错误 - ASP.NET MVC "'Object reference not set to an instance of an object.'" Error 在ASP.NET表单提交中,它引发未设置为对象实例的Object引用。 - On ASP.NET form submit it throws an Object reference not set to an instance of an object.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM