Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象引用未设置为对象xml C的实例,并使用C在xml文件中添加一些节点_C#_.net_Xml - Fatal编程技术网

C# 对象引用未设置为对象xml C的实例,并使用C在xml文件中添加一些节点

C# 对象引用未设置为对象xml C的实例,并使用C在xml文件中添加一些节点,c#,.net,xml,C#,.net,Xml,这是在C中使用xml的简单代码 <table1 name="something"> <column1 someattribute="something"> actualname </column> </table1> 我尝试了下面的代码,但它抛出了一个对象引用未设置为对象xml C实例的异常 请帮忙 附言:假设表1中有许多列,那么首先我想累加每一列,然后将其添加到xdocument,然后是表2,我想对他做同样的事情。您的XML结构中没有表1

这是在C中使用xml的简单代码

<table1 name="something">
   <column1 someattribute="something"> actualname </column>
</table1>
我尝试了下面的代码,但它抛出了一个对象引用未设置为对象xml C实例的异常 请帮忙 附言:假设表1中有许多列,那么首先我想累加每一列,然后将其添加到xdocument,然后是表2,我想对他做同样的事情。

您的XML结构中没有表1元素。这意味着您的document.Elementtable1表返回null,因此在对其调用.Addelem时出现异常

调试NullReferenceExceptions在大多数情况下都是非常基本的,这些问题可以通过使用调试器单步执行代码轻松解决

作为参考,元素法

获取文档顺序中具有指定XName的第一个子元素

这就是为什么会得到空值

初始化表单时,会创建一个空文档,就像新建XDocument一样。然后,在单击按钮时,您正试图使用选择器document.Elementtable1将新元素作为子元素添加到table1中。这就是问题所在,您的XDocument是空的!您需要首先创建table1元素,或者直接将elem对象添加到文档中

这意味着您可以这样做以确保表1存在:


抛出异常,因为在XDocumentdocument XElement element element element表中插入项目时,该元素尚不存在

您正在尝试将now元素添加到不存在的现有元素中:

document.Element("table1").Add(elem);//throws exception object refernce not set ..."
table1元素尚未添加到文档中。您需要引用相应的父元素。要将其添加到根元素,请使用

document.Root.Add(elem);

每个XML文档/结构都必须有一个唯一的节点/标记,称为根节点,该节点/标记在每个文档中只存在一次。因此,一个合适的xml可以如下所示-一个根,但多个节点:

<?xml version="1.0" encoding="UTF-8"?>
<root name="example">
  <node attribute="1">information 1</node>
  <node attribute="2">information 2</node>
</root>
使用LINQ2XML访问根元素有两种可能:和。 了解这一点并使用给定的xml代码,应该很容易遵循以下使用LINQ2XML操作xml代码的示例:

// every XML structure has to have a root node, that is unique
// and should exists only once!
var xmlCode = @"<root name=""example"">
                    <node attribute=""1"">information 1</node>
                    <node attribute=""2"">information 2</node>
                </root>";
// load the example xml code
var document = XDocument.Parse(xmlCode);
// create new attribute object
var newAttribute = new XAttribute("attribute", "3");
// create new node / column object with the newly created attribute and set the content of the node
var newNode = new XElement("node", newAttribute, "information 3");
// add the newly created node to the XML Document root
// in LINQ2XML the root node can be accessed via the Root property
// of an XDocument object
document.Root.Add(newNode);
// alternative: find the document root node using its name, in this example the node 
// is named "root" and add the new element to it!
// document.Element("root").Add(newNode);
输出为:

<root name="example">
  <node attribute="1">information 1</node>
  <node attribute="2">information 2</node>
  <node attribute="3">information 3</node>
</root>
在您的问题中,table1是这个xml代码的根,column1的等价物是节点


如果要使用框架提供的功能,则。

看起来像是一个基本的NullRef异常。检查document.ElementTable是否返回实际值,例如var tableElement=document.ElementTable;tableElement.addelem请解释一下我正在通过C学习XML,你能把代码读一遍吗again@user3113029我添加了一些详细信息显示您是否会创建下面的代码关于C数据首先我们通过xelement elem=new xelementDB,new XattributeName,someinput添加一些根;然后我会把添加新节点和修改现有节点混为一谈,谢谢!你有什么困惑吗?看看我在我的问题中添加了什么,你应该在那里找到你的答案。我知道了,但是如果我想在这个元素中添加新元素,那么我应该怎么做?意味着我没有插入任何东西??First document.Root.Addelem;然后是docemnt.element列,someattribute。。不是吗?不,将其添加到相应的父级。您正在尝试将您正在创建的table1元素添加到一个显然不存在的现有table1元素中。如果我想在现有节点中添加一些子元素,您会选择什么?构建整个树然后将其添加到文档更有效-您只是尝试将其添加到一个还不存在的位置,因为您还没有添加它。
<?xml version="1.0" encoding="UTF-8"?>
<root name="example">
  <node attribute="1">information 1</node>
  <node attribute="2">information 2</node>
</root>
// every XML structure has to have a root node, that is unique
// and should exists only once!
var xmlCode = @"<root name=""example"">
                    <node attribute=""1"">information 1</node>
                    <node attribute=""2"">information 2</node>
                </root>";
// load the example xml code
var document = XDocument.Parse(xmlCode);
// create new attribute object
var newAttribute = new XAttribute("attribute", "3");
// create new node / column object with the newly created attribute and set the content of the node
var newNode = new XElement("node", newAttribute, "information 3");
// add the newly created node to the XML Document root
// in LINQ2XML the root node can be accessed via the Root property
// of an XDocument object
document.Root.Add(newNode);
// alternative: find the document root node using its name, in this example the node 
// is named "root" and add the new element to it!
// document.Element("root").Add(newNode);
<root name="example">
  <node attribute="1">information 1</node>
  <node attribute="2">information 2</node>
  <node attribute="3">information 3</node>
</root>