never_odd_or_even
never_odd_or_even

Reputation: 314

Encode String As XML

I have an XML document I need to post to a web service. Example file below:

<Budgets>
    <Budgetline LocationName="Test Location 1" LocationCode="TL1"  Period="201705" Amount="1276.000000"/>
    <Budgetline LocationName="Test Location 2" LocationCode="TL2"  Period="201705" Amount="1530.050000"/> </Budgets>

I need to post this data to a web service as encoded XML. Example below:

&lt;Budgets&gt;
    &lt;Budgetline LocationName=&quot;Test Location 1&quot; LocationCode=&quot;TL1&quot;  Period=&quot;201705&quot; Amount=&quot;1276.000000&quot;/&gt;
    &lt;Budgetline LocationName=&quot;Test Location 2&quot; LocationCode=&quot;TL2&quot;  Period=&quot;201705&quot; Amount=&quot;1530.050000&quot;/&gt; &lt;/Budgets&gt;

I'm reading the file as a string and the converting it to XML using:

[string]$budgetsfile = Get-Content "D:\Upload\TestData.xml" -Raw
$xmlBudgets = ConvertTo-Xml $budgetsfile -As Stream -NoTypeInformation

However the XML data is being wrapped with an object element <Object><Objects> XMLdata </Object></Objects>, which the web service throws back an error for.

Is there a way to encode the data as XML without the <Object><Objects> tags. Or should I just use string.replace() to remove them.

The soap envelope needs to look something like this:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://example.com/XMLSchema-instance" xmlns:xsd="http://example.com/2001/XMLSchema" xmlns:soap12="http://example.com/2003/05/soap-envelope">
  <soap12:Body>
    <upload xmlns="http://example.com">
      <authUser>$user</authUser>
      <authPass>$pass</authPass>
      <xmlData>&lt;Budgets&gt;
    &lt;Budgetline LocationName=&quot;Test Location 1&quot; LocationCode=&quot;TL1&quot;  Period=&quot;201705&quot; Amount=&quot;1276.000000&quot;/&gt;
    &lt;Budgetline LocationName=&quot;Test Location 2&quot; LocationCode=&quot;TL2&quot;  Period=&quot;201705&quot; Amount=&quot;1530.050000&quot;/&gt;
&lt;Budgets&gt;</xmlData>
    </upload>
  </soap12:Body>
</soap12:Envelope>

Upvotes: 1

Views: 3567

Answers (3)

user3564747
user3564747

Reputation: 21

An alternative solution is:

[xml]$xmlBudgets = Get-Content "D:\tmp\TestData.xml" -Raw
[string]$encoded = 
[System.Security.SecurityElement]::Escape($xmlBudgets.OuterXml)

Upvotes: 2

never_odd_or_even
never_odd_or_even

Reputation: 314

Thanks for everyone's input. The solution was to read the XML file as a string and encode it using Abhijith pk HtmlEncode suggestion but on the string instead of an XML node.

[string]$xmlBudgets = Get-Content "D:\Upload\TestData.xml" -Raw
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::HtmlEncode($budgetsfile)

The soap envelope looks like this.

[xml]$SOAP_upload = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://example.com/XMLSchema-instance" xmlns:xsd="http://example.com/2001/XMLSchema" xmlns:soap12="http://example.com/2003/05/soap-envelope">
  <soap12:Body>
    <upload xmlns="http://example.com">
      <authUser>$user</authUser>
      <authPass>$pass</authPass>
      <xmlData>'+$encoded+'</xmlData>
    </upload>
  </soap12:Body>
</soap12:Envelope>'

Upvotes: 3

ClumsyPuffin
ClumsyPuffin

Reputation: 4059

You can directly cast the data into xml:

[xml]$xmlBudgets = Get-Content "D:\Upload\TestData.xml" -Raw

for encoding it , you can use:

Add-Type -AssemblyName System.Web
#below command will create the encoded string and assign it to variable
$encoded = [System.Web.HttpUtility]::HtmlEncode($xmlBudgets.Innerxml)

for the soap envelope you can use this:

$head = @"
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://example.com/XMLSchema-instance" xmlns:xsd="http://example.com/2001/XMLSchema" xmlns:soap12="http://example.com/2003/05/soap-envelope">
  <soap12:Body>
    <upload xmlns="http://example.com">
      <authUser>$user</authUser>
      <authPass>$pass</authPass>
      <xmlData>
"@

$tail = @"
</xmlData>
    </upload>
  </soap12:Body>
</soap12:Envelope>
"@

$head + $encoded +$tail

Upvotes: 2

Related Questions