1

Here's the XML:

<PolicyChangeSet schemaVersion="2.1" username="" description="">
    <Attachment name="" contentType="">
        <Description/>
        <Location></Location>
    </Attachment>
</PolicyChangeSet>

I'm just trying to add the value "XXX" between the "location" tag.

I tried this but it isn't working:

    string newValue = string.Empty;
    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load(AppVars.pxCentralXMLPayloadFilePath);
    node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/location");

    node.InnerText = "XXX";

    xmlDoc.Save(AppVars.pxCentralXMLPayloadFilePath);

What am I missing?!

3 Answers 3

0

doc.Save(@"XML FILE PATH"); //This will save the changes to the file.

1
  • sorry, saving the file is in my original code. I updated the question. I still get a null exception at node.InnerText = "XXX";.
    – JJ.
    Aug 10, 2012 at 15:22
0

Please use iteration through ChildNodes of XmlDocument - as described in walkthrough from MS Support site. According to documentation of SelectSingleNode() method:

The XmlNode should not be expected to be connected "live" to the XML document. That is, changes that appear in the XML document may not appear in the XmlNode, and vice versa.

1
  • sorry, saving the file is in my original code. I updated the question. I still get a null exception at node.InnerText = "XXX";.
    – JJ.
    Aug 10, 2012 at 15:22
0

Found out what the problem was CASE SENSITIVITY.

1
  • Has found the reason - modifications does not have effect as result of SelectSingleNode is not expected to be connected with source XmlDocument - please see updated answer. Aug 10, 2012 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.