【问题标题】:How do we remake program to open xml file and read percent value after price word and change this value from我们如何重新制作程序以打开 xml 文件并在价格字后读取百分比值并将此值从
【发布时间】:2020-03-21 13:43:02
【问题描述】:

我们如何重新制作程序以在 windows 窗体中打开 xml 文件并在文件 MIK_RT.xml 中的价格词之后读取百分比值,并且可以从 windows 窗体中更改此百分比值并通过单击确定按钮以新值保存文件?

你能在 windows 窗体中重新制作我的程序或在 windows 窗体中插入这个 powershell 代码吗?文件需要以utf8编码保存

$outputFile = 'MIK_RT.xml'
write-output $outputFile
[xml](Get-Content "$outputFile") | ForEach-Object {$_.SelectNodes('//FieldCostOptions/IncreaseCost') |ForEach-Object {$_.GetAttribute("Percent")}; write-output $node.percent }
$inputpercent = Read-Host -Prompt 'percent'

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

用于编辑 MIK_RT.xml 的文件

<?xml version="1.0" encoding="utf-8"?>
<PriceConfiguration Name="MIK_RT" Version="1">
    <Description></Description>
    <AuthorID>s0011373519</AuthorID>
    <LastEditedBy>s0011373519</LastEditedBy>
    <LastUpdate>14.11.2019 12:56:27</LastUpdate>
    <File Type="XLS" Engine="0">
        <FilesMask>E:\FTP\PRICE_FORCEAUTO_MIK\RT.xls</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>2</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>Артикул (EXIST_RT)</ReplaceTableName>
                </Field>
                <Field index="2" Enable="1">
                    <Name>Наименование</Name>
                    <SheetColumn>3</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>1</SheetColumn>
                    <Required>1</Required>
                    <ValueMode>0</ValueMode>
                    <KeepValue>0</KeepValue>
                    <KeepLevel>0</KeepLevel>
                    <FieldFunctions Enabled="0" Version="1" />
                    <FieldConditions Enable="0" Version="1" />
                    <ReplaceTableName>Производитель (FA_RT)</ReplaceTableName>
                </Field>
                <Field index="4" Enable="1">
                    <Name>Наличие</Name>
                    <SheetColumn>4</SheetColumn>
                    <Required>1</Required>
                    <ValueMode>0</ValueMode>
                    <KeepValue>0</KeepValue>
                    <KeepLevel>0</KeepLevel>
                    <FieldFunctions Enabled="1" Version="1">
                        <Function CodeName="Replace" Enabled="1" param1="&gt;" param2="" />
                    </FieldFunctions>
                    <FieldConditions Enable="0" Version="1" />
                </Field>
                <Field index="5" Enable="1">
                    <Name>Цена</Name>
                    <SheetColumn>5</SheetColumn>
                    <Required>1</Required>
                    <ValueMode>0</ValueMode>
                    <KeepValue>0</KeepValue>
                    <KeepLevel>0</KeepLevel>
                    <FieldFunctions Enabled="1" Version="1">
                        <Function CodeName="CompareNumbers" Enabled="1" param1="2499" param2="1000000" />
                    </FieldFunctions>
                    <FieldConditions Enable="0" Version="1" />
                    <FieldCostOptions Enabled="1" Version="1">
                        <IncreaseCost Enabled="1" Percent="-5" />
                    </FieldCostOptions>
                </Field>
                <Field index="6" Enable="1">
                    <Name>ЦенаЗакупа</Name>
                    <SheetColumn>5</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>RT</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>4</FirstRow>
            <LastRowColumn>1</LastRowColumn>
        </SheetConfiguration>
    </Sheets>
</PriceConfiguration>

【问题讨论】:

  • previous question 一样,您在不属于它们的地方添加空格字符。 $_.SelectNodes(' //FieldCostOptions/IncreaseCost') --> $_.SelectNodes('//FieldCostOptions/IncreaseCost')
  • 我们还没有收到您的来信.. 我的回答解决了您的问题吗?如果是这样,请点击左侧的✓ 考虑accepting。这将帮助其他有类似问题的人更轻松地找到它。

标签: java c# c++ powershell


【解决方案1】:

您应该在变量中捕获 xml,以便能够操作节点和/或属性。 如下所示:

$inputFile  = 'D:\MIK_RT.xml'
$outputFile = 'D:\Output.xml'
# read the xml from file into a variable
[xml]$xml = Get-Content $inputFile -Raw

# find and write out the current percentage(s)
$currentPercent = $xml.SelectNodes('//FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") }
Write-Output $currentPercent

$newPercent = Read-Host -Prompt 'percent'

# update the xml percent attribute(s)
$xml.SelectNodes('//FieldCostOptions/IncreaseCost') | ForEach-Object { $_.SetAttribute("Percent", $newPercent)}
# save the updated xml
$xml.Save($outputFile)

如果您希望文件以 UTF-8 编码保存,您可以这样做:

用于编写为 UTF-8 带有 BOM

$sw = [System.IO.StreamWriter]::new($outputFile, $false, [System.Text.Encoding]::UTF8)

用于编写 UTF-8 BOM (默认情况下,StreamWriter 会创建没有 BOM 的 UTF-8 编码文件)

$sw = [System.IO.StreamWriter]::new($outputFile, $false)

或者先创建一个编码对象:

$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
$sw = [System.IO.StreamWriter]::new($outputFile, $false, $utf8NoBom)

现在使用 StreamWriter 对象保存文件

$xml.Save($sw)
$sw.Dispose()

【讨论】:

  • 输出必须是utf8,否则用户无法以本地语言阅读
  • @danrather 我已经编辑了我的答案并添加了保存为 UTF-8 的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 2019-02-17
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
相关资源
最近更新 更多