Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#&;XML-对象引用未设置为对象的实例_C#_Asp.net_Xml_Visual Studio 2010 - Fatal编程技术网

C#&;XML-对象引用未设置为对象的实例

C#&;XML-对象引用未设置为对象的实例,c#,asp.net,xml,visual-studio-2010,C#,Asp.net,Xml,Visual Studio 2010,晚上好,飞越者, 我最近通过VisualStudio2010开始用XML编码。我遇到了一个看似简单的解决方案,但我却没有找到。我得到一个对象引用未设置的错误,但我看不到我没有设置的内容。(此处错误:) 我的代码隐藏: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; us

晚上好,飞越者,
我最近通过VisualStudio2010开始用XML编码。我遇到了一个看似简单的解决方案,但我却没有找到。我得到一个对象引用未设置的错误,但我看不到我没有设置的内容。(此处错误:)

我的代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
  {
    int intDVDID;
    XmlDocument myXmlDocument = new XmlDocument();
    XmlNode rootNode;
    XmlNode selectedDVD;

public void Page_Load(object Src, EventArgs E)
{
    intDVDID = Convert.ToInt32(Request.QueryString["id"]);

    myXmlDocument.Load(Request.PhysicalApplicationPath + @"dvd.xml");
    rootNode = myXmlDocument.DocumentElement;
    selectedDVD = rootNode.ChildNodes[intDVDID - 1];
    if (!Page.IsPostBack)
    {
        rootNode.RemoveChild(selectedDVD);
        myXmlDocument.Save(Request.PhysicalApplicationPath + @"dvd.xml");
        lblMessage.Text = "You have successfully deleted the DVD";
    }
  }
}
只是说:

  int intDVDID = new intDVDID 
我知道,通过阅读这篇文章,你们都会因为我缺乏经验和不了解如何解决这个问题而大发雷霆,但我很感谢你们花时间和耐心去寻找

致以最良好的祝愿, 劳拉:)

编辑: 以下是我的XML:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This XML document describes a DVD library -->
<library>
  <DVD id="1">
    <title>Breakfast at Tiffany's</title>
    <format>Movie</format>
    <genre>Classic</genre>
  </DVD>
  <DVD id="2">
    <title>Contact</title>
    <format>Movie</format>
    <genre>Science fiction</genre>
  </DVD>
  <DVD id="3">
    <title>Little Britain</title>
    <format>TV Series</format>
    <genre>Comedy</genre>
  </DVD>
</library>

蒂芙尼早餐酒店
电影
经典
接触
电影
科幻小说
小英国
电视剧
喜剧片

看起来您选择的虚拟磁盘可能为空,请进行一些空检查以验证

if(selectedDVD != null)
{
}
编辑:在评论中回答您的问题。下面是一些示例代码。我加入了一个xpath,尽管看起来您的案例非常简单,但您可能希望在将来使用它

string xml = "<xml><node id='1'></node><node id='2'></node></xml>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);           

//This is an xpath(This replaces your .DocumentElement.ChildNodes[index]
XmlNode desiredNode = xmlDoc.DocumentElement.SelectSingleNode("node[@id='1']");
if (desiredNode != null)
{
    xmlDoc.DocumentElement.RemoveChild(desiredNode);
}//if             
stringxml=”“;
XmlDocument xmlDoc=新的XmlDocument();
LoadXml(xml);
//这是一个xpath(它取代了.DocumentElement.ChildNodes[index]
XmlNode desiredNode=xmlDoc.DocumentElement.SelectSingleNode(“node[@id='1']);
if(desiredNode!=null)
{
xmlDoc.DocumentElement.RemoveChild(desiredNode);
}//如果

请确保您的查询字符串不是空的,这可能会导致您获取的引用项为空

if(Request.QueryString["id"]!="")
{
    intDVDID = Convert.ToInt32(Request.QueryString["id"]);
}

dvd.xml
看起来像什么?编辑后添加了xml。谢谢你的提问!当你得到这个错误时,
id
的值是多少?它是1。我不知道发生了什么。谢谢你。我现在要测试这个。我感谢你的耐心。好的,所以我进行了一些空检查,我仍然得到了相同的结果。在更改中为了让自己看起来比我已经做的还要愚蠢…要做一个空检查,我只是做这样的事情?:`if(selectedDVD!=null){selectedDVD=rootNode.ChildNodes[intDVDID-1];}“非常感谢你,布莱恩。我真的非常感谢你的帮助。你是一个明星!非常感谢你的帮助!你的帮助太棒了。帮助我解决了一些问题。非常感谢!