Xml Documnt error

I have a data set and want to append it to a xml file. When I try to do this I get this error

The node to be inserted is from a different document context

the code i use below is

void SaveDataSet(DataTable dt, string StationName, ref string err)

{

XmlNodeList allnodes;

DataSet ds = new DataSet(StationName);

const string ApparatusName = "ICX001.xml";

XmlDocument StationsXML = new XmlDocument();

XmlDocument xmlTempDoc = new XmlDocument();

XmlNode TempNode;

XmlNode root;

string filename = "Temp.xml";

string path = @"C:\Program Files\Harland Medical\ " + ApparatusName;

err = "No erros";

ds.Merge(dt);

try

{

StationsXML.Load(path);

ds.WriteXml(filename);

xmlTempDoc.Load(filename);

TempNode = xmlTempDoc.DocumentElement;

root = StationsXML.DocumentElement;

root.LastChild.AppendChild(TempNode);

StationsXML.Save(path);

}

catch (Exception ex)

{

err = ex.ToString();

}

}



Answer this question

Xml Documnt error

  • zerovelocity

    When you insert a node from a different XmlDocument, you have to use the XmlDocument.ImportNode method. Please see the linked MSDN page for details and a code sample.

    Best regards,
    Anton


  • Xml Documnt error