Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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# 将XElement添加到XDocument时出现NullReferenceException_C#_Nullreferenceexception_Xelement - Fatal编程技术网

C# 将XElement添加到XDocument时出现NullReferenceException

C# 将XElement添加到XDocument时出现NullReferenceException,c#,nullreferenceexception,xelement,C#,Nullreferenceexception,Xelement,我有以下.xml文件: <?xml version="1.0" encoding="utf-8" ?> <Params> <Name>Resolver1</Name> <RemoteHosts> <Host> <Name>Locale</Name> <IP>localhost</IP> <Port>8082&l

我有以下.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<Params>
  <Name>Resolver1</Name>
  <RemoteHosts>
    <Host>
      <Name>Locale</Name>
       <IP>localhost</IP>
       <Port>8082</Port>
     </Host>
  </RemoteHosts>
</Params>
反正

xmlList.Save("NetConfig.xml");

工作正常,保存新项…有什么问题吗?

XmlList只包含1个节点及其
参数
而不是
远程主机
尝试
XmlList.Root.Element(“远程主机”)
是否可以调试以找出实际为空的节点?是h变量,还是“RemoteHosts”节点?不清楚。您保存的文件是否包含新的
主机
节点?@ek_ny xmlList.Element(“RemoteHosts”)为空..嗯,您调用的
添加
的对象为空,并且您添加的节点实际上已添加。这没有意义。谢谢!现在使用xmlList.Element(“Params”).Element(“RemoteHosts”).Add。。。它起作用了!
xmlList.Save("NetConfig.xml");