Reading a value from an XML

Got a problem you cannot solve? Try here.
AdrianGray
Posts: 32
Joined: Fri Jul 25, 2014 5:21 am

Reading a value from an XML

Postby AdrianGray » Fri Jul 25, 2014 5:44 am

I am trying to read a value from a web.config file (XML format) as shown below. How do I retrieve the CURRENT_VERSION value with the file written in the following format. I am happy to get the whole "ADD" key and parse it. Get XML File Settings does not appear to work on this format and I am unable to find any examples of "Evaluate XPath Settings" which may help. I can read the entire file however I am hoping there is a better way. Thanks, Adrian.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="DB" value="SQL"/>
<add key="ShowDefaultSettingUserMenu" value="False"/>
<add key="CURRENT_VERSION" value="2014.06.25" />
<add key="COMPANY_NAME" value="New School"/>
<add key="QuickLinkShowImage" value="false"/>
</appSettings>
</configuration>

AdrianGray
Posts: 32
Joined: Fri Jul 25, 2014 5:21 am

Re: Reading a value from an XML

Postby AdrianGray » Sun Jul 27, 2014 10:22 pm

I have managed to get the element by using XPath query //configuration/appSettings/add[@key="CURRENT_VERSION"] with the Result Type "Full XML String". Although this is great for reading it, I still have to parse the string to get the value attribute from it. Once done, I cannot re-write in a new value without reading in the file, then re-writing it. Is there a way to change an attribute of an element within an XML. Thanks, Adrian.

FrancescoT
Site Admin
Posts: 5361
Joined: Sun Aug 22, 2010 4:28 am

Re: Reading a value from an XML

Postby FrancescoT » Mon Jul 28, 2014 2:57 am

Dear Adrian,

Supposing you have;

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationSettings>
  <Section Name="General">
    <Key Name="RootPath" Value="C:\mytest\sales" />
    <Key Name="Images" Value="\Jpg\" />
  </Section>
  <Section Name="Connection">
    <Key Name="Server" Value="SERVER04" />
    <Key Name="SqlDatabase" Value="PM4Uster" />
    <Key Name="TrustedConnection" Value="0" />
  </Section>
  <Section Name="Agent">
    <Key Name="UseAgent" Value="0" />
  </Section>
  <Users>
    <key id="1" />
  </Users>
</ApplicationSettings>


To modify "<Key Name="UseAgent" Value="0" /> to <Key Name="UseAgent" Value="1111" /> :

Code: Select all

~InstallAware Clipboard Data~
~Edit XML File~
~{E57A1E67-DE0C-4F73-A5CE-64F0739CC7D8}~
~C:\XMLFile2.xml~
~UTF-8~
~0~
~ApplicationSettings\Section\Key~
~~
~"Name,UseAgent","Value,1111"~
~1~
~FALSE~
~FALSE~
~FALSE~


Hope this helps you

Regards
Francesco Toscano
InstallAware Software

White Papers (HowTos) - http://www.installaware.com/publication ... papers.htm
Publications - http://www.installaware.com/publications-review.htm
InstallAware Help -F1 anywhere in the InstallAware IDE

AdrianGray
Posts: 32
Joined: Fri Jul 25, 2014 5:21 am

Re: Reading a value from an XML

Postby AdrianGray » Tue Jul 29, 2014 4:16 pm

I am not sure what I am doing wrong, however I have spent considerable time trying to get this to work without success. I have copied your solution and tried to modify it to match my file as posted above, without success. Are you able to see what I am doing wrong based on the file I have shown above. Thanks, Adrian.

~InstallAware Clipboard Data~
~Edit XML File~
~{DB7DFCE4-FB67-454E-B34D-8ECB11F4711A}~
~$TARGETDIR$\TestWeb.config~
~UTF-8~
~0~
~configuration\appSettings\add~
~~
~"Key,CURRENT_VERSION","Value,1111"~
~1~
~FALSE~
~FALSE~
~FALSE~

FrancescoT
Site Admin
Posts: 5361
Joined: Sun Aug 22, 2010 4:28 am

Re: Reading a value from an XML

Postby FrancescoT » Wed Jul 30, 2014 11:18 am

Dear Adrian,

... just a question, are you sure the XML file is effectively available on target system when you try to update it?

In other words, if the file is installed by your package, you should use the Edit XML File after "Apply Install" execution.

BTW, it seems you are using the correct parameters with command.

Regards
Francesco Toscano
InstallAware Software

White Papers (HowTos) - http://www.installaware.com/publication ... papers.htm
Publications - http://www.installaware.com/publications-review.htm
InstallAware Help -F1 anywhere in the InstallAware IDE

AdrianGray
Posts: 32
Joined: Fri Jul 25, 2014 5:21 am

Re: Reading a value from an XML

Postby AdrianGray » Thu Jul 31, 2014 7:35 pm

The file I am testing against is in a fixed location and is not part of the installed files. I am just trying to read the key from it prior to installing other files. Can you please confirm if you are able to read the value from the file content I posted. Thanks, Adrian.

FrancescoT
Site Admin
Posts: 5361
Joined: Sun Aug 22, 2010 4:28 am

Re: Reading a value from an XML

Postby FrancescoT » Fri Aug 01, 2014 10:49 am

Please try with the attached project.

Regards
Attachments
Xmltest.rar
(2.13 MiB) Downloaded 610 times
Francesco Toscano
InstallAware Software

White Papers (HowTos) - http://www.installaware.com/publication ... papers.htm
Publications - http://www.installaware.com/publications-review.htm
InstallAware Help -F1 anywhere in the InstallAware IDE

AdrianGray
Posts: 32
Joined: Fri Jul 25, 2014 5:21 am

Re: Reading a value from an XML

Postby AdrianGray » Sun Aug 03, 2014 6:04 pm

Hi Francesco. Thanks for the sample project, however it did not work for me as it relies on knowing the original value within the "value=" attribute is in order to delete it. The situation I have is that I wish to replace the "ADD" element which has the attribute Key="CURRENT_VERSION" with a new value="NEW_VALUE", WITHOUT knowing what original value= was. It could be any number of values. To delete the key, it appears that you need to know what the "Value" already is, I do not. Without deleting it, the create/update does not appear to work for me. Is there a way to delete the "Add" element with the "CURRENT_VERSON" attribute without specifying its "Value" attribute. This is my issue. Thanks again, Adrian.

FrancescoT
Site Admin
Posts: 5361
Joined: Sun Aug 22, 2010 4:28 am

Re: Reading a value from an XML

Postby FrancescoT » Mon Aug 04, 2014 11:24 am

Dear Adrian,

if I haven't missed anything, I don't think is possible.
It looks impossible to identify such key ... it can be anything. Am I wrong?

Regards
Francesco Toscano
InstallAware Software

White Papers (HowTos) - http://www.installaware.com/publication ... papers.htm
Publications - http://www.installaware.com/publications-review.htm
InstallAware Help -F1 anywhere in the InstallAware IDE

AdrianGray
Posts: 32
Joined: Fri Jul 25, 2014 5:21 am

Re: Reading a value from an XML

Postby AdrianGray » Thu Aug 07, 2014 3:41 am

Hi Francesco. I can find it by doing the following...

~InstallAware Clipboard Data~
~Evaluate XPath Query~
~{5AB84784-AC4C-4F19-84EF-CE95CE5C8ACB}~
~$TARGETDIR$\web.config~
~//configuration/appSettings/add[@key="CURRENT_VERSION"]~
~InstalledVersion~
~0~

This gives me the complete element into a variable (InstalledVersion). I then read the web.config file into a variable, parse it by finding <add key="CURRENT_VERSION", then parse the second part of the parse again to the first >

I then join the two halves of the file together but insert the new key between <add key="CURRENT_VERSION" value="2014.08.07" />. I then rewrite the file. I was hoping to PARSE the variable on with $InstalledVersion$ but adding a variable in the parse statement didn't work for me. This seems like a long way to update an XML file's element whereby I just want to insert a new date into the value=" attribute of it. Thanks for your ongoing help, Adrian

FrancescoT
Site Admin
Posts: 5361
Joined: Sun Aug 22, 2010 4:28 am

Re: Reading a value from an XML

Postby FrancescoT » Fri Aug 08, 2014 11:39 am

Dear Adrian,

with the complete element into a variable, are you not able to extract the element name (... via Parse String) that could be used with the Edit XML command to update the Key?

Regards
Francesco Toscano
InstallAware Software

White Papers (HowTos) - http://www.installaware.com/publication ... papers.htm
Publications - http://www.installaware.com/publications-review.htm
InstallAware Help -F1 anywhere in the InstallAware IDE

gibbie99
Posts: 26
Joined: Fri Jan 14, 2011 9:57 am

Re: Reading a value from an XML

Postby gibbie99 » Fri Jul 31, 2015 2:11 pm

This is probably not ideal to parse the string if you dont have to. this works. Basically pull in the attribute @value then under result type use node value.


~InstallAware Clipboard Data~
~MessageBox~
~{4D9F8B1B-AA2F-448A-A504-C81D683C5D3A}~
~~
~port is $PORTONE$~
~0~
~1~
~~
~Evaluate XPath Query~
~{1B70793D-F1BF-473C-87C3-31CEE8E810FA}~
~c:\neil.xml~
~//configuration/appSettings/add[@key="Topology.Server.Port"]/@value~
~PORTONE~
~2~


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 21 guests