Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

powershell - how do i make program that modify value percent in file MIK_Neva.xml

how do i remake this program to enter value of percent from power shell prompt? how do i modify this code to enter percent value from powershell prompt

$inputpercent = Read-Host -Prompt 'percent:'
$outputFile = 'MIK_Neva.xml'
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false)
$sw = New-Object System.IO.StreamWriter($outputFile, $false, $utf8WithoutBom)
[xml](Get-Content "$outputFile") | ForEach-Object {$_.SelectNodes('//FieldCostOptions/IncreaseCost') | ForEach-Object {$_.SetAttribute("Percent", $inputpercent)};#  $_.Save('Output.xml')}


$xml.Save( $sw )
$sw.Close()
}

and got an error

PS C:  program> $ inputpercent = Read-Host -Prompt 'percent:'
$ outputFile = 'MIK_Neva.xml'
$ utf8WithoutBom = New-Object System.Text.UTF8Encoding ($ false)
$ sw = New-Object System.IO.StreamWriter ($ outputFile, $ false, $ utf8WithoutBom)
[xml] (Get-Content "$ outputFile") | ForEach-Object {$ _. SelectNodes ('// FieldCostOptions / IncreaseCost') | ForEach-Object {$ _. SetAttribute ("Percent", $ inputpercent)}; # $ _. Save ('Output.xml')}


$ xml.Save ($ sw)
$ sw.Close ()
}
percent :: 8
Cannot call a method for an expression with a NULL value.
line: 5 character: 52
+ ... ach-Object {$ _. SelectNodes ('// FieldCostOptions / IncreaseCost') | ForE ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~~~~~~~~
     + CategoryInfo: InvalidOperation: (:) [], RuntimeException
     + FullyQualifiedErrorId: InvokeMethodOnNull

file MIK_Neva.xml

 <?xml version="1.0" encoding="utf-8"?>
<PriceConfiguration Name="MIK_Neva" Version="1"><Description></Description><AuthorID>s0011373519</AuthorID><LastEditedBy>s0011373519</LastEditedBy><LastUpdate>14.11.2019 12:56:45</LastUpdate><File Type="XLS" Engine="0"><FilesMask>E:FTPPRICE_FORCEAUTO_MIKNevaAvtocom.xlsx</FilesMask><IncludeFiles></IncludeFiles><ExcludeFiles></ExcludeFiles><Macro OnStart=""/><XLSoptions><Password></Password><MergeXLSsheets>1</MergeXLSsheets></XLSoptions><CSVoptions><RowsSeparator>13-10</RowsSeparator><ColumnsSeparator>59</ColumnsSeparator><AddHeaderRow>0</AddHeaderRow><UseBrackets>0</UseBrackets><Encoding>windows-1251</Encoding></CSVoptions><ReplaceTable Name="Price Codes" IsEmpty="False"><Replacement Find="{@КодПрайса}" ReplaceWith=""/><Replacement Find="{@ИмяФайлаДляПрайса}" ReplaceWith=""/></ReplaceTable></File><Sheets><SheetConfiguration index="1"><Name>Обработчик листа</Name><Description></Description><SheetSelectMode>1</SheetSelectMode><SheetName>остатки Микадо</SheetName><SheetIndex>1</SheetIndex><ReplaceTable Name="Price Sheet Codes" IsEmpty="False"><Replacement Find="{@КодЛиста}" ReplaceWith=""/><Replacement Find="{@ИмяФайлаДляЛиста}" ReplaceWith=""/></ReplaceTable><PriceFields><Field index="1" Enable="1"><Name>Артикул</Name><SheetColumn>1</SheetColumn><UseCellText>1</UseCellText><Required>1</Required><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/><ReplaceTableName>Артикул (EMEX_Neva)</ReplaceTableName></Field><Field index="2" Enable="1"><Name>Наименование</Name><SheetColumn>2</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/><ReplaceTableName>Наименование (FA)</ReplaceTableName></Field><Field index="3" Enable="1"><Name>Производитель</Name><SheetColumn>5</SheetColumn><Required>1</Required><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="4" Enable="1"><Name>Наличие</Name><SheetColumn>3</SheetColumn><Required>1</Required><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="5" Enable="1"><Name>Цена</Name><SheetColumn>4</SheetColumn><Required>1</Required><ValueMode>0</ValueMode><KeepValue>1</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="1" Version="1"><Function CodeName="CompareNumbers" Enabled="1" param1="3000" param2="1000000"/></FieldFunctions><FieldConditions Enable="1" Version="1"/><FieldCostOptions Enabled="1" Version="1"><IncreaseCost Enabled="1" Percent="-5"/></FieldCostOptions></Field><Field index="6" Enable="1"><Name>ЦенаЗакупа</Name><SheetColumn>4</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="7" Enable="1"><Name>ПР</Name><SheetColumn>1</SheetColumn><ValueMode>1</ValueMode><Formula>NevaAvtocom</Formula><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="8"><Name>Срок_ZZAP</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="9"><Name>КодСтраны</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="10"><Name>Страна</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="11"><Name>ГТД</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="12"><Name>ИНН_Поставщика</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="13"><Name>КПП_Поставщика</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="14"><Name>НомерН</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="15"><Name>ДатаН</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="16"><Name>НомерСчета</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="17"><Name>ДатаСчета</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="18"><Name>НамерСФ</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field><Field index="19"><Name>ДатаСФ</Name><SheetColumn>1</SheetColumn><ValueMode>0</ValueMode><KeepValue>0</KeepValue><KeepLevel>0</KeepLevel><FieldFunctions Enabled="0" Version="1"/><FieldConditions Enable="0" Version="1"/></Field></PriceFields><FirstRow>2</FirstRow><LastRowColumn>1</LastRowColumn></SheetConfiguration></Sheets></PriceConfiguration>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your code, using ".Percent" - Object's that returned from SelectSingleNode is not parsed to some class with field Percent. It's only XmlNodes that has methods to work wot atributes You should use

x.SelectSingleNode ('// FieldCostOptions / IncreaseCost').SetAttribute("Percent", -5)

Please, or you can use this example to execute all in one line:

[xml](Get-Content "input.xml") | ForEach-Object {$_.SelectNodes('// FieldCostOptions/IncreaseCost') | ForEach-Object {$_.SetAttribute("Percent", -5)}; $_.Save('Output.xml')}

If you want to write a script, is it something you want?

$inputFile = Read-Host -Prompt 'Input file name to process'
$outputFile = Read-Host -Prompt 'Output file name'
$xml = New-Object -TypeName XML
$xml.Load($inputFile)

$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false)
$sw = New-Object System.IO.StreamWriter($outputFile, $false, $utf8WithoutBom)

$xml.Save( $sw )
$sw.Close()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...