none
Problem importing a xml file RRS feed

  • Question

  • Hello,

    I am currently trying to develop a script which searchs a folder for XML-files, which then get checked one by one if they contain a certain code, and if so they will then get moved into another folder.

    My Code Looks like this:

    Set-Location C:\Users\censored\Desktop\Response
    $xmlcount = Get-ChildItem -file | Measure-Object | %{$_.count}
    for($ii=0; $ii -lt $xmlcount-1; $ii++)
    {
        [xml]$current_file = Get-ChildItem -file -path C:\Users\censored\Desktop\Response | Select-Object -First 1
        if($current_file.Response.ResponseCode = 0)
        {
            Move-Item -path $current_file -destination .\Archive
        }
    }

    Now my problem seems to be this line:

    [xml]$current_file = Get-ChildItem -file -path C:\Users\censored\Desktop\Response | Select-Object -First 1

    Since if I let the script run it only throws out following error message:

    The value "20190614_censored_20190614_140824_20190615_040507.xml" cannot be converted into "System.Xml.XmlDocument". 
    Error: "The selected node cannot be inserted as a valid subordinate node, since the selected node is of the wrong type."
    In line:1 Chracter:1
    + [xml]$current_file = Get-ChildItem -file -path \\dmzkon02\Billing_K6_ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
        + FullyQualifiedErrorId : RuntimeException

    (I translated some part of the error, hope everything is understandable.)

    Anyone having a clue why this is the case and happens? The document is definitly a XML-file..

    Sincerly,

    cysx

    Note: If you find any more mistakes or better alternatives please tell me, I'd like to improve my coding since I'm new to PowerShell.

    Tuesday, June 18, 2019 9:59 AM

Answers

  • You cannot get aa file as XML.  You have to use "Get-Content".

    Get-ChildItem C:\Users\censored\Desktop\Response\*.xml -file |
        ForEach-Object{
            [xml]$current_file = Get-Content $_
            if ($current_file.Response.ResponseCode -eq 0) {
                Move-Item $_ -destination C:\Users\censored\Desktop\Response\Archive
            }
        }
    

    Use full paths and use paths correctly.

    I suggest taking the time to learn PowerShell instead of just guessing.  Almost every line was bad because you don't have even the simplest knowledge of PowerShell.  This will cause you frustration and a huge amount of wasted time.

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell

          PowerShell Documentation

          PowerShell Style   Guidelines


    \_(ツ)_/

    • Marked as answer by cysx Tuesday, June 18, 2019 11:20 AM
    Tuesday, June 18, 2019 11:01 AM
    Moderator

All replies

  • You cannot get aa file as XML.  You have to use "Get-Content".

    Get-ChildItem C:\Users\censored\Desktop\Response\*.xml -file |
        ForEach-Object{
            [xml]$current_file = Get-Content $_
            if ($current_file.Response.ResponseCode -eq 0) {
                Move-Item $_ -destination C:\Users\censored\Desktop\Response\Archive
            }
        }
    

    Use full paths and use paths correctly.

    I suggest taking the time to learn PowerShell instead of just guessing.  Almost every line was bad because you don't have even the simplest knowledge of PowerShell.  This will cause you frustration and a huge amount of wasted time.

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell

          PowerShell Documentation

          PowerShell Style   Guidelines


    \_(ツ)_/

    • Marked as answer by cysx Tuesday, June 18, 2019 11:20 AM
    Tuesday, June 18, 2019 11:01 AM
    Moderator
  • Thank you very much. Having to learn it all by myself as just an intermediately taught student is not that easy as I got that task as part of an internship with PowerShell never been used by me before. Since then I only used to code in HTML and PHP with SQL coming next after my internship ends. Well to not tell you my entire resume I only felt a little offended by your answer since I don't have any other possibility else than trying since my supervision is having his holidays.

    Nevertheless I am very thankful since now that this is a solution, I can start understanding it. And afterwards I will have some time to learn PowerShell. Thank you very much!

    Sincerly,

    cysx

    Tuesday, June 18, 2019 11:27 AM
  • You can learn PowerShell in a weekend by using the links I posted.

    A professional should never feel offended by criticism from senior engineers.  Listen to those with more experience.  Don't see criticism as a personal issue.  To be a tech means to learn by researching the technology you are trying to use.

    If you were really a programmer in  the areas you claim then you would know how to learn basic PowerShell syntax in less than an hour.  Until you learn how to do these basic things you will not be able to advance.


    \_(ツ)_/

    Tuesday, June 18, 2019 11:34 AM
    Moderator