title: Powershell Getting Error Cannot convert value "System.Xml.XPathNodeList" to type "System.Xml.XmlDocument
body: <p>I have following powershell code, which is selecting a xml node which has namespace.</p>
  
  <pre><code>$ns = new-Object System.Xml.XmlNamespaceManager $doc.NameTable
  $ns.AddNamespace("dns", "http://www.nlog-project.org/schemas/NLog.xsd")
  $obj3 = $doc.SelectNodes('//dns:nlog',$ns)
  </code></pre>
  
  <p>But I am getting following error:</p>
  
  <blockquote>
    <p>Cannot convert value "System.Xml.XPathNodeList" to type
    "System.Xml.XmlDocument". Error: "The specified node cannot be
    inserted as  the valid child of this node, because the specified node
    is the wrong type." At
    C:\MetacubeAutoBuildDeployment\PowershellScripts\PEStandAloneWebConfigReplace.ps1:15
    char:1
    + $obj3 = $doc.SelectNodes('//dns:nlog',$ns)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
        + FullyQualifiedErrorId : RuntimeException</p>
  </blockquote>
  
  <p>Is there any specific reason for this error?
  I Have to fetch the value of connectionString tag highlighted in the $doc.
  $doc Contains:</p>
  
  
  
  <pre><code>&lt;configuration&gt;
    &lt;configSections&gt;
      &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
      &lt;section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" /&gt;
      &lt;section name="ciel" type="Ciel.Application.Common.CielConfigSectionHandler, Ciel.Application" /&gt;
      &lt;sectionGroup name="elmah"&gt;
        &lt;section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" /&gt;
        &lt;section name="errorLog" requirePermission="false" type="Elmah.FallbackErrorLogSectionHandler, Elmah.FallbackErrorLog" /&gt;
        &lt;section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" /&gt;
        &lt;section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" /&gt;
      &lt;/sectionGroup&gt;
    &lt;/configSections&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
    &lt;/startup&gt;
    &lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;!-- Load NLog extensions from the *.dll file --&gt;
      &lt;extensions&gt;
        &lt;add assembly="NLog.Web" /&gt;
        &lt;add assembly="NLog.Extended" /&gt;
      &lt;/extensions&gt;
      &lt;!-- Targets or Output --&gt;
      &lt;targets&gt;
        &lt;default-wrapper xsi:type="AsyncWrapper" /&gt;
        &lt;!-- File Target --&gt;
        &lt;target name="sqllog" xsi:type="File" fileName="App_Data/NLoggerLogs/PESql.log" archiveFileName="App_Data/log/CielSql.{#####}.log" maxArchiveFiles="100" archiveAboveSize="10485760" archiveNumbering="Sequence" concurrentWrites="true" keepFileOpen="false" encoding="UTF-8"&gt;
          &lt;layout xsi:type="CSVLayout" delimiter="Tab"&gt;
            &lt;column name="level" layout="${level:uppercase=true}" /&gt;
            &lt;column name="aspnet-sessionid" layout="${aspnet-sessionid}" /&gt;
            &lt;column name="aspnet-request(ASP.NET_SessionId)" layout="${aspnet-request:cookie=ASP.NET_SessionId}" /&gt;
            &lt;column name="aspnet-request(HTTP_USER_AGENT)" layout="${aspnet-request:serverVariable=HTTP_USER_AGENT}" /&gt;
            &lt;column name="aspnet-request(REMOTE_ADDR)" layout="${aspnet-request:serverVariable=REMOTE_ADDR}" /&gt;
            &lt;column name="aspnet-request(REMOTE_HOST)" layout="${aspnet-request:serverVariable=REMOTE_HOST}" /&gt;
            &lt;column name="date" layout="${longdate}" /&gt;
            &lt;column name="message" layout="${message:exceptionSeparator=|:withException=true}" /&gt;
            &lt;column name="aspnet-user-identity" layout="${aspnet-user-identity}" /&gt;
          &lt;/layout&gt;
        &lt;/target&gt;
        &lt;target name="debuglog" xsi:type="File" fileName="App_Data/NLoggerLogs/PE.log" archiveFileName="App_Data/log/Ciel.{#####}.log" maxArchiveFiles="100" archiveAboveSize="2097152" archiveNumbering="Sequence" concurrentWrites="true" keepFileOpen="false" encoding="UTF-8"&gt;
          &lt;layout xsi:type="CSVLayout" delimiter="Tab"&gt;
            &lt;column name="date" layout="${longdate}" /&gt;
            &lt;column name="level" layout="${level:uppercase=true}" /&gt;
            &lt;column name="asp-application" layout="${asp-application}" /&gt;
            &lt;column name="asp-request" layout="${asp-request:cookie=String:serverVariable=String:queryString=String:item=String:form=String}" /&gt;
            &lt;column name="asp-session" layout="${asp-session:variable=String}" /&gt;
            &lt;column name="aspnet-sessionid" layout="${aspnet-sessionid}" /&gt;
            &lt;column name="aspnet-session" layout="${aspnet-session:variable=Sring}" /&gt;
            &lt;column name="aspnet-application" layout="${aspnet-application:variable=String}" /&gt;
            &lt;column name="aspnet-request" layout="${aspnet-request:cookie=String:serverVariable=String:queryString=String:item=String:form=String}" /&gt;
            &lt;column name="aspnet-request(ASP.NET_SessionId)" layout="${aspnet-request:cookie=ASP.NET_SessionId}" /&gt;
            &lt;column name="aspnet-request(HTTP_USER_AGENT)" layout="${aspnet-request:serverVariable=HTTP_USER_AGENT}" /&gt;
            &lt;column name="aspnet-request(URL)" layout="${aspnet-request:serverVariable=URL}" /&gt;
            &lt;column name="aspnet-request(REMOTE_ADDR)" layout="${aspnet-request:serverVariable=REMOTE_ADDR}" /&gt;
            &lt;column name="aspnet-request(REMOTE_HOST)" layout="${aspnet-request:serverVariable=REMOTE_HOST}" /&gt;
            &lt;column name="aspnet-user-authtype" layout="${aspnet-user-authtype}" /&gt;
            &lt;column name="aspnet-user-identity" layout="${aspnet-user-identity}" /&gt;
            &lt;column name="threadid" layout="${threadid}" /&gt;
            &lt;column name="stacktrace" layout="${stacktrace:topFrames=2}" /&gt;
            &lt;column name="machinename" layout="${machinename}" /&gt;
            &lt;column name="document-uri" layout="${document-uri}" /&gt;
            &lt;column name="callsite" layout="${callsite:className=true:fileName=true:includeSourcePath=true:methodName=true}" /&gt;
            &lt;column name="message" layout="${message:exceptionSeparator=|:withException=true}" /&gt;
          &lt;/layout&gt;
        &lt;/target&gt;
        &lt;target name="accesslog" xsi:type="File" fileName="${basedir}/APP_Data/NLoggerLogs/PEAccess.log" archiveFileName="${basedir}/App_Data/log/CielAccess.{#####}.log" maxArchiveFiles="100" archiveAboveSize="2097152" archiveNumbering="Sequence" concurrentWrites="true" keepFileOpen="false" encoding="UTF-8"&gt;
          &lt;layout xsi:type="CSVLayout" delimiter="Tab"&gt;
            &lt;column name="aspnet-sessionid" layout="${aspnet-sessionid}" /&gt;
            &lt;column name="aspnet-request(HTTP_USER_AGENT)" layout="${aspnet-request:serverVariable=HTTP_USER_AGENT}" /&gt;
            &lt;column name="aspnet-request(REMOTE_ADDR)" layout="${aspnet-request:serverVariable=REMOTE_ADDR}" /&gt;
            &lt;column name="aspnet-request(REMOTE_HOST)" layout="${aspnet-request:serverVariable=REMOTE_HOST}" /&gt;
            &lt;column name="date" layout="${longdate}" /&gt;
            &lt;column name="message" layout="${message:exceptionSeparator=|:withException=true}" /&gt;
          &lt;/layout&gt;
        &lt;/target&gt;
        &lt;target name="database" xsi:type="Database"&gt;
          &lt;!--
          Remarks:
            The appsetting layouts require the NLog.Extended assembly.
            The aspnet-* layouts require the NLog.Web assembly.
            The Application value is determined by an AppName appSetting in Web.config.
            The "NLogDb" connection string determines the database that NLog write to.
            The create dbo.Log script in the comment below must be manually executed.
          --&gt;
          &lt;!--&lt;connectionStringName&gt;SQLServer_develop&lt;/connectionStringName&gt;--&gt;
          &lt;dbProvider&gt;System.Data.SqlClient&lt;/dbProvider&gt;
          **&lt;connectionString&gt;server=server;database=database;integrated security=False;User ID=userid;Password=password&lt;/connectionString&gt;**
          &lt;commandText&gt;
            insert into [CIEL].[DPR_JOBINSTANCEDETAILS] (
            JOBID, JOBINSTANCEID, STEPINSTANCEID,STEPID,LOGTYPE, MESSAGETYPE, MESSAGE, PARENTID,LOGGEDON
            )
            values (
            @JOBID, @JOBINSTANCEID, @STEPINSTANCEID, @STEPID, @LOGTYPE,@MESSAGETYPE, @MESSAGE, @PARENTID, @LOGGEDON
            );
          &lt;/commandText&gt;
          &lt;parameter name="@JOBID" layout="${event-properties:item=JOBID}" /&gt;
          &lt;parameter name="@JOBINSTANCEID" layout="${event-properties:item=JOBINSTANCEID}" /&gt;
          &lt;parameter name="@STEPINSTANCEID" layout="${event-properties:item=STEPINSTANCEID}" /&gt;
          &lt;parameter name="@STEPID" layout="${event-properties:item=STEPID}" /&gt;
          &lt;parameter name="@LOGTYPE" layout="${event-properties:item=LOGTYPE}" /&gt;
          &lt;parameter name="@MESSAGETYPE" layout="${event-properties:item=MESSAGETYPE}" /&gt;
          &lt;parameter name="@MESSAGE" layout="${event-properties:item=MESSAGE}" /&gt;
          &lt;parameter name="@PARENTID" layout="${event-properties:item=PARENTID}" /&gt;
          &lt;parameter name="@LOGGEDON" layout="${date}" /&gt;
        &lt;/target&gt;
        &lt;target name="sqllog_table" xsi:type="Database"&gt;
          &lt;!--Remarks:
            The appsetting layouts require the NLog.Extended assembly.
            The aspnet-* layouts require the NLog.Web assembly.
            The Application value is determined by an AppName appSetting in Web.config.
            The "NLogDb" connection string determines the database that NLog write to.
            The create dbo.Log script in the comment below must be manually executed.--&gt;
          &lt;connectionStringName&gt;NLogConnection&lt;/connectionStringName&gt;
        &lt;/target&gt;
      &lt;/targets&gt;
      &lt;!-- Rules for calling the appropriate Target --&gt;
      &lt;rules&gt;
        &lt;logger name="DebugLogger" minlevel="Debug" writeTo="debuglog" /&gt;
        &lt;logger name="SqlLogger" minlevel="Debug" writeTo="sqllog" /&gt;
        &lt;logger name="AccessLogger" minlevel="Info" writeTo="accesslog" /&gt;
        &lt;logger name="PROCESSENGINEDB" minlevel="Trace" writeTo="database" /&gt;
      &lt;/rules&gt;
    &lt;/nlog&gt;
  &lt;/configuration&gt;
  </code></pre>