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

标签 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.

谁能帮我找出错误的原因

最佳答案

你不能给自己添加一个节点

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

关于c# - 无法在xml中的指定位置插入节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330610/

相关文章:

c# - Azure Active Directory token 缓存 C# 控制台应用程序

c# - LINQ 到 SQL : Delete entity (by ID) with one query

java - XMLReader 搜索相对于主应用程序的 DTD 文件而不是 XML 文件

xml - 将创建的运行时节点名称传递给xsl:value-of select

python:通过网络发送列表/字典

c# - 用于查找最近的前一个兄弟的 XPath 语句

c# - 为什么这里不能隐含类型参数的泛型(有两个类型参数)?

xml - 解析带有重复标签的 XML 文件

hadoop - Avro 序列化和 Avro 格式的区别

java - Serializable/Cloneable/... 的惯用设计在 Scala 中看起来如何?