0

I have the following .xml file:

<?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>

Now, when I try to add another "Host" in the "RemoteHosts" section using the following code it raises a NullReferenceException:

XDocument xmlList = XDocument.Load("NetConfig.xml");

xmlList.Element("RemoteHosts").Add(new XElement("Host",    
new XElement("Name", h.name),
new XElement("IP", h.IP),
new XElement("Port", h.port)));

anyway the

xmlList.Save("NetConfig.xml");

works well, saving the new item...what's wrong?

Rick
  • 13
  • 3

2 Answers2

1

XmlList contains only 1 node and its Params not RemoteHosts

Piotr Auguscik
  • 3,651
  • 1
  • 22
  • 30
  • Thank you! Now using xmlList.Element("Params").Element("RemoteHosts").Add ... it works! – Rick Jun 19 '11 at 11:33
0

Try xmlList.Root.Element("RemoteHosts")

Shurdoof
  • 1,719
  • 13
  • 16