Trying to append a xml file but it

Hi I keep getting a object reference not set to instance. I can't spot what i'm doing wrong. i know it is the doc.

 if (File.Exists("ArtistList.xml")) { string IDartist = ID.ToString(); //file name string filenames = filename; //create new instance of xmldocument XmlDocument doc = new XmlDocument(); //create a artist Node and Add value XmlNode node = doc.CreateNode(XmlNodeType.Element, "Artist", null); // create a Id node and add value XmlNode ArtistID = doc.CreateElement("ArtistID"); ArtistID.InnerText = IDartist; // create a artist Name node and add value XmlNode ArtistNameNode = doc.CreateElement("ThisArtistName"); ArtistNameNode.InnerText = name; //Create a artist Address node and add value XmlNode ArtistAddress = doc.CreateElement("ThisArtistAddres"); ArtistAddress.InnerText = address; //Add to parent Node node.AppendChild(ArtistID); node.AppendChild(ArtistNameNode); node.AppendChild(ArtistAddress); //add to elements collection doc.DocumentElement.AppendChild(node); //save back doc.Save(filenames); } 

by barcemo via /r/csharp

Leave a Reply