0

I have one excel file where we have two column, both are linked with each other & I also have one xml file where I want to feed those column data & want to create 1 xml for each group, like 1st xml will take away group 20 as 20.xml

enter image description here

xml file content is - Highlighted in bold will be target area of xml to update the excel data in it.

<?xml version="1.0" encoding="UTF-8"?>
    <SingleAction>
        <Title>Setting Data Group - **20**</Title>
        <Relevance>(if exists property "in proxy agent context" then (not in proxy agent context) else true)</Relevance>
        
        code..
        
        code..
        

        <Parameter Name="Data ID">**20**</Parameter>
        <Settings>
        lots of other settings
        settings
        setttings
        ..
        ..
    
        <Target>
            <Data1Column>**NameA**</Data1Column>
            <Data1Column>**NameB**</Data1Column>
        </Target>
3
  • Have you tried anything? Did your attempt fail with an error or did you get unexpected results? Commented Aug 31, 2020 at 12:32
  • I have no coding experience at all but with whatever available online help, I always manage my stuff but this is something out of scope however I have tried looking for many forum but no post touching this in whole. I have approx 50000 rows with many groups so thinking automate approach rather going with manual stuff. Commented Aug 31, 2020 at 12:49
  • Automation sure seem like the way to go. As for examples there should be plenty, but perhaps not for your exact scenario, however it should be possible to piece something together from different examples. The problem with your question in its current form is that it comes across more as an order (and this isn't a script writing service) than a programming question. Commented Aug 31, 2020 at 18:46

1 Answer 1

0

The first thing to do would be to save the Excel file as a .csv file (make sure to choose .csv comma delimited).

Then you would want to have an xml template. It would look like this:

<?xml version="1.0" encoding="UTF-8"?>
    <SingleAction>
        <Title>Setting Data Group - **##PARAMNAME##**</Title>
        <Relevance>(if exists property "in proxy agent context" then (not in proxy agent context) else true)</Relevance>
        
        code..
        
        code..
        

        <Parameter Name="Data ID">**##PARAMNAME##**</Parameter>
        <Settings>
        lots of other settings
        settings
        setttings
        ..
        ..
    
        <Target>
            ##DATACOLUMNS##
        </Target>

Then we would replace the ##PLACEHOLDERS## with actual data. In this powershell script, I assume that the headers, Data1 and Data2, have been changed to data1 and data2.

$data = Import-Csv .\data.csv
$luTable = @{}

# Create Keys in Lookup Table
$data | % {
    if (!$luTable.ContainsKey("$($_.data2)")) { $luTable["$($_.data2)"] = New-Object System.Collections.ArrayList }
}

$luTable.Keys | % {
    $key = $_ # Store Key
    $data | where data2 -Match $_ | select data1 | % {
        $luTable[$key].Add($_.data1)
    }
}

# Build XML Files

$luTable.Keys | % {
    $key = $_

    $filetext = gc ".\template.xml"
    $filetext = $filetext.Replace("##PARAMNAME##", $key)

    $targets = ""
    
    $luTable[$key] | % {
        $targets += "<DataColumn1>$($_)</DataColumn1>"
    }

    $filetext = $filetext.Replace("##DATACOLUMNS##", $targets)

    sc -Path ".\$($key).xml" -Value $filetext
}

That should do the trick!

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

2 Comments

Thats perfect @erwijet, you are awesome !!!! the only thing problematic is below datacolumn & their in between value ##DATACOLUMNS## should come in next row not in single row, currently script is dumping all value in single line. <DataColumn1>"##DATACOLUMNS##"</DataColumn1> <DataColumn1>"##DATACOLUMNS##"</DataColumn1> <DataColumn1>"##DATACOLUMNS##"</DataColumn1> <DataColumn1>"##DATACOLUMNS##"</DataColumn1> <DataColumn1>"##DATACOLUMNS##"</DataColumn1>
@Vijaykhurava, To add line breaks in powershell, on the line $targets += "<DataColumn1>$($_)</DataColumn1>" add `n so the line becomes $targets += "<DataColumn1>$($_)</DataColumn1>`n".

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.