1

I'm finding it hard to figure out a way to retrieve content from XML file. Below is how my xml file looks like. I'm trying to retrieve the complete 'nlog' node. Please help.

<configuration>
<configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, ..."/>
 </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <variable name="LoggingDirectory" value="D:/Logging/"/>
      <include file="${LoggingDirectory}Config/Framework.nlog.xml"/>
  </nlog>
 </configuration>

here is what I have tried so far:

$nlogConfigFile = 'D:\machine.config.nlog.xml'
$nlogConfigXml = new-object xml
$nlogConfigXml.Load($nlogConfigFile);
$nlogConfigXml.PreserveWhitespace = $true

I have used the "Get-XmlNode" function provided in this blog http://blog.danskingdom.com/powershell-functions-to-get-an-xml-node-and-get-and-set-an-xml-elements-value-even-when-the-element-does-not-already-exist/

Get-XmlNode -XmlDocument $nlogConfigXml -NodePath "configuration.configSections.section[@name='nlog']"     ## works OK
Get-XmlNode -XmlDocument $nlogConfigXml -NodePath "configuration.nlog"   ## does NOT work

I have also tried "Select-Xml" , .SelectSingleNode commands but none of them seem to work. Please let me know if I'm missing something.

3 Answers 3

5

This works:

$nlogConfigXml = [xml]$(gc "D:\machine.config.nlog.xml")

Then you can navigate $nlogConfigXml using object notation.

For example, doing this:

$nlogConfigXml.configuration.nlog.variable.name

...outputs this:

LoggingDirectory
Sign up to request clarification or add additional context in comments.

3 Comments

Hi How do I access an element by attribute using this model ? For example: retrieve the section with name= 'nlog' ?
@CrazyFrog - I've added an example.
For Powershell noobs (I'm currently one of them) - gc is short for Get-Content, not garbage collector.
0

I would suggest using Select-Xml and XPath. Mind that you need to include namespace info to make it work correctly:

$Xml = [xml]@'
<configuration>
<configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, ..."/>
 </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <variable name="LoggingDirectory" value="D:/Logging/"/>
      <include file="${LoggingDirectory}Config/Framework.nlog.xml"/>
  </nlog>
</configuration>
'@

Select-Xml -Xml $Xml -Namespace @{
    n = "http://www.nlog-project.org/schemas/NLog.xsd"
} -XPath //n:nlog

Namespace definition (hashtable value) is just copy/paste of xmlns. The name you specify (hashtable key) is the same you later have to use in XPath queries as a prefix for XPath elements (as in example: n:nlog)

Comments

0
$nlogConfigFile = '.\machine.config.nlog.xml'
[XML]$xmlFileContent = Get-Content $nlogConfigFile
$xmlFileContent.configuration.nlog.variable.name

Just a little bit different format from the answer before.

1 Comment

This doesn't work properly it always have been throwing the error. The answer specified by @enigmitavity was helpful and working as expected.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.