I am creating xml from c# code.I am gettign the following error: Cannot insert the node in the specified location.
My code to do so is:
try {
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(docNode);
// XmlNode openerpNode = doc.CreateElement("open");
doc.AppendChild(openerpNode);
XmlElement dataNode = doc.CreateElement("dataex");
openerpNode.AppendChild(dataNode);
doc.PrependChild(colName.GenerateColumnsForTable("code", doc, dataNode)); //THIS LINE CAUSES ERROR AND THIS FUNCTON RETURNS A XmlNode TYPE OBJECT "dataNode"
//I use PrependChild here because i will call this function again passing another string in first parameter and it should attach the same xml
Console.WriteLine("string is : " + doc);
Console.ReadKey();
doc.Save("C:/cod.xml");
return true;
} catch (Exception ex) {
Console.WriteLine("The error is :" + ex);
Console.ReadKey();
return false;
}
public class ReturnColumnName
{
public XmlNode GenerateColumnsForTable(string tableName, XmlDocument doc, XmlNode dataNode)
{
//Here i am using the same doc and dataNode to create xml
return dataNode;
}
}
EDIT: I changed the code from this
doc.PrependChild(colName.GenerateColumnsForTable("code_pays_iso", doc, dataNode));
to
XmlNode nod = colName.GenerateColumnsForTable("code_colisage", doc,dataNode);
doc.AppendChild(doc.OwnerDocument.ImportNode(nod, true));
Now it gives this error :
The error is :System.NullReferenceException: Object reference not set to an instance of an object.
Could some one please help me in finding the cause of error