1

I'm trying to update a node value from a XML file but when I'm trying to do this action is throwing an exception "Additional information: Object reference not set to an instance of an object."

This is what I have:

public void UpdateXMLValues(List<string> values)
{
    XmlNode node;
    xmldoc = new XmlDocument();
    xmldoc.Load(XMLInterfaces);
    node = xmldoc.SelectSingleNode("Servers/MYSERVER");
    XMLValues = new List<string>(values);
    node.Attributes["Host"].Value = XMLValues[0];
    xmldoc.Save(XMLInterfaces);
}

and this is my XML information:

<Servers>
  <MYSERVER>
    <Host>0.0.0.0</Host>
    <Port>23</Port>
    <User>TestingUser</User>
    <Password>/NNfWRStbZsUyc88S5tjhA==</Password>
  </MYSERVER>
</Servers>

When I press F11 in the line node.Attributes["Host"].values = XMLValues[0]; I'm getting the error

any idea?

Javier Salas
  • 882
  • 2
  • 13
  • 30

1 Answers1

2

Host is not an attribute of the MYSERVER element. It's an element inside it.

You can access it like so:

var hostNode = node["Host"];
hostNode.Value = XMLValues[0];

For reference, if it was an attribute, your XML would look like this:

<MYSERVER host="some_host">
</MYSERVER>
RB.
  • 33,692
  • 12
  • 79
  • 121