2

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

xav xav
  • 231
  • 2
  • 5
  • 12
  • Does it help if instead of passing doc and dataNode by value you pass them by reference and don't return: public void GenerateColumnsForTable(string tableName, ref XmlDocument doc, ref XmlNode dataNode) – Neal Sep 01 '15 at 12:05
  • Its doen reference worked for me :) – xav xav Sep 01 '15 at 12:24

1 Answers1

0

You can't add a node to itself

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                ReturnColumnName colName = new ReturnColumnName();
                string input = "<?xml version=\"1.0\"?><open></open>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(input);
                XmlElement opener = (XmlElement)doc.GetElementsByTagName("open")[0];

                XmlElement dataNode = doc.CreateElement("dataex");

                XmlElement child = (XmlElement)colName.GenerateColumnsForTable("code", doc, dataNode);
                if (opener.ChildNodes.Count == 0)
                {
                    opener.AppendChild(child);
                }
                else
                {
                    opener.PrependChild(child);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The error is :" + ex);
                Console.ReadKey();
            }

        }
        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;
            }
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20