I'm trying to create an XML document to return through a web service, and am quite stumped. I'm still pretty new at XML itself, so I'm trying to learn as I go. The error I'm getting is
object reference not set to an instance of an object.
The code works by calling a constructor, taking in the request document and parsing it to the response. I have the format of both the request and the response, and just need to be able to send it back.
Code:
XmlTextReader xml_read 开发者_高级运维= new XmlTextReader(HttpContext.Current.Request.MapPath("/ap/aitcXMLConfirmationRequest.xml"));
XmlDocument xml_doc = new XmlDocument();
xml_doc.Load(xml_read);
xml_read.Close();
//Do some stuff.
int int_dtl = 1;
//Builds the list of Confirmation items.
XmlNodeList nodelst_cnfrm = p_transdoc.SelectNodes("//Request/OrderRequest/ItemOut");
foreach (XmlNode node in nodelst_cnfrm)
{
XmlNode node_cnfrm_itm = this.CreateElement("ConfirmationItem");
//Do some other stuff here
}
xml_doc.ImportNode(node_cnfrm_itm,true);
root.AppendChild(xml_doc); //Error generated here.
this.AppendChild(root);
But it's giving me the aforementioned error. Can anyone help out? I'm not understanding how there is no instance of an object, if I have been manipulating it before the AppendChild request.
Any ideas?
With respect to NullReferenceExceptions in general, you should just put a break point (typically F9) on that line and start the Debugger. Once that line is hit, inspect the variables and confirm that one is in fact null.
In your case, it should be pretty obvious that root
is null (given the code successfully uses xml_doc
). At that point, find the places where root
is supposed to be set and investigate why that isn't happening.
精彩评论