From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Why doesn't this replace the XML in my file?

I am trying to write some code that replaces some XML in a file. The first part works (opening the file, locating the XML, replacing it). When I save the file, it contains the original XML not the modified XML.

 

I used the Get XML method to retrieve the text I wanted to change, Setting the Value property of the node doesn't seem to work but I don't know what else to try - I need the equivalent of a 'Set XML' invoke node but I haven't found it.

 

Any ideas? Am I even close?

(I'm new to XML and manipulating it in LabVIEW and I'm glazing over now so ideas which don't involve reading another 20 documents would be really appreciated, thank you Smiley Happy )

 

0 Kudos
Message 1 of 7
(3,218 Views)

You are taking a roundabout way to changing that value.

 

In the meantime, I will not tell you to read 20 documents, but really, learn about XPath and Get First/All Matched Node.  I am still smacking my head uncontrollably from the sight of getting the XML text simply to do string matching.  I still can not unsee it....

 

In your case '1' is not the value of the node you found, the first child of that node is a text node with value '1'.  You get used to it.

 

SimpleXPathExample1.png

Message 2 of 7
(3,197 Views)

Well, Darin was first and I had same idea with XPath... So I'll post it anyway, just for the sake of effort taken 😛

xml.png

 

Actually, using XPath does not require "20 documents", it is quite easy, so I'd recommend you to take a look at it if you're going to use XML.

Message 3 of 7
(3,184 Views)

Thanks Darrin.K and PiDi,

 

I looked at using that method but I didn't manage to get a clear understanding of how to use XPath. I found an article which looked simple at first sight but I couldn't get the hang of how the structure of my real XML mapped onto the XPath elements. And I never managed to get anything back unless I supplied * as a search pattern.

 

Anyway - I'll try this method again and see if I can a guide that helps me more (easily!)

 

Smiley Happy

0 Kudos
Message 4 of 7
(3,131 Views)

If you'll follow detailed help of Get First Matched Node VI, you'll eventually find this document: http://www.w3.org/TR/xpath/ . To do most of the common searches examples from the beginning of chapter 2 are more than sufficient 😉

Message 5 of 7
(3,120 Views)

Thank you! (I used Darin's code but only because I got to it first)

 

So now I have a node for which the XML is modified. I thought saving this to a new file would give me a duplicate of the original file but with a replaced property value...... but no! My new file contains the old XML despite 'Get XML' displaying the correct version.

 

Is there something different I should do?

 

[ This snippet reveals my true motives! ]

Replace XML in file snippet 2.png

0 Kudos
Message 6 of 7
(3,090 Views)

It seems that there is error in XML Save function, which does not save updated XML. See this topic: http://forums.ni.com/t5/LabVIEW/Save-XML-File-Error/td-p/1555028 - and follow Darins suggestion from two years back again 😉

0 Kudos
Message 7 of 7
(3,070 Views)