无法在xml中的指定位置插入节点

时间:2015-09-01 11:36:28

标签: c# xml serialization xml-serialization

我正在从c#代码创建xml。我遇到以下错误:     无法在指定位置插入节点。

我这样做的代码是:

  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;
        }
    }

编辑: 我改变了这个

的代码
 doc.PrependChild(colName.GenerateColumnsForTable("code_pays_iso", doc, dataNode));   

 XmlNode nod = colName.GenerateColumnsForTable("code_colisage", doc,dataNode);
 doc.AppendChild(doc.OwnerDocument.ImportNode(nod, true));

现在它出现了这个错误:

The error is :System.NullReferenceException: Object reference not set to an instance of an object.

有人可以帮助我找出错误原因

1 个答案:

答案 0 :(得分:0)

您无法将节点添加到自身

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;
            }
        }
    }
}
​