0

I am trying to read XML content from which is stored in a text file. But it is not working properly. Please go through my code as mention below:

Write-Host 'Welcome'
$path='D:\SOAP\PozEncoXML.txt'
$xml=[xml](Get-Content -path $path)
Write-Host 'File Content'
write-host $xml
$node= $xml.Envelope.Body.uploadFileToUcm.document.Content='Hello world'
Write-Host $node

In line no 3 of the above code I am trying to read XML content from the text file. If I remove [xml] then Get-Content is working fine. But need that content in XML format. Can anyone help me to overcome this issue?

This is the content of my file content

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://xmlns.oracle.com/apps/financials/commonModules/shared/financialUtilService/types/" xmlns:fin="http://xmlns.oracle.com/apps/financials/commonModules/shared/financialUtilService/">
<soapenv:Header/>
<soapenv:Body>
  <typ:uploadFileToUcm>
     <typ:document>
        <fin:Content></fin:Content>
        <fin:FileName>xxxxx.csv</fin:FileName>
        <fin:ContentType>csv</fin:ContentType>
        <fin:DocumentTitle>Journal Import</fin:DocumentTitle>
        <fin:DocumentAuthor>xxxx</fin:DocumentAuthor>
        <fin:DocumentSecurityGroup>FAFusionImportExport</fin:DocumentSecurityGroup>
        <fin:DocumentAccount>fin$/generalLedger$/import$</fin:DocumentAccount>
        <fin:DocumentName>?</fin:DocumentName>
        <fin:DocumentId>?</fin:DocumentId>
     </typ:document>
  </typ:uploadFileToUcm>
</soapenv:Body>
</soapenv:Envelope>

Thank you in Advance

4
  • 3
    "But it is not working properly" - what is happening instead? For example, does your code continue to run with unexpected results, or do you see an error message? Note this line is probably incorrect $xml.Envelope.Body.uploadFileToUcm.document.Content='Hello world' - the second = is an assignment not a comparison which might mean the rest of your program doesn't behave how you expect... Commented Sep 9, 2022 at 10:26
  • Actually my xml file is containing a SOAP format, using that format I have to send data to UCM. This code is working other server. Commented Sep 9, 2022 at 10:57
  • Have 2 variables, one with the source text, and one converted to an xmldocument object. write-host badly tries to convert the xmldocument object to text. Commented Sep 9, 2022 at 14:15
  • Hmm, $xml.innerxml and $xml.outerxml properties seem to have the source text. Commented Sep 9, 2022 at 14:26

2 Answers 2

1

The following works here:

[xml] $x = gc infile.xml
$x.Envelope.Body.uploadFileToUcm.document.content='Hello World'
$x.save("$((pwd).path)\outfile.xml")
gc outfile.xml

Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://xmlns.oracle.com/apps/financials/commonModules/shared/financialUtilService/types/" xmlns:fin="http://xmlns.oracle.com/apps/financials/commonModules/shared/financialUtilService/">
  <soapenv:Header />
  <soapenv:Body>
    <typ:uploadFileToUcm>
      <typ:document>
        <fin:Content>Hello World</fin:Content>
        <fin:FileName>xxxxx.csv</fin:FileName>
        <fin:ContentType>csv</fin:ContentType>
        <fin:DocumentTitle>Journal Import</fin:DocumentTitle>
        <fin:DocumentAuthor>xxxx</fin:DocumentAuthor>
        <fin:DocumentSecurityGroup>FAFusionImportExport</fin:DocumentSecurityGroup>
        <fin:DocumentAccount>fin$/generalLedger$/import$</fin:DocumentAccount>
        <fin:DocumentName>?</fin:DocumentName>
        <fin:DocumentId>?</fin:DocumentId>
      </typ:document>
    </typ:uploadFileToUcm>
  </soapenv:Body>
</soapenv:Envelope>
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of save into a file $x.save("$((pwd).path)\outfile.xml"), can we store it into a variable?
Just use .ToString() .
@ShivShankarMaiti: so you want the xml text? Perhaps $x.outerxml is what you're looking for?
0

assuming the file is well formatted as xml, I would create a new instance of a xml object and use the load method:

$path='D:\SOAP\PozEncoXML.txt'
$xml = New-Object -TypeName xml
$xml.Load($path)
[System.Xml.Linq.XDocument]::Parse($Xml.OuterXml).ToString()

Output

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://xmlns.oracle.com/apps/financials/commonModules/shared/financialUtilService/types/" xmlns:fin="http://xmlns.oracle.com/apps/financials/commonModules/shared/financialUtilService/">
  <soapenv:Header />
  <soapenv:Body>
    <typ:uploadFileToUcm>
      <typ:document>
        <fin:Content>Hello world</fin:Content>
        <fin:FileName>xxxxx.csv</fin:FileName>
        <fin:ContentType>csv</fin:ContentType>
        <fin:DocumentTitle>Journal Import</fin:DocumentTitle>
        <fin:DocumentAuthor>xxxx</fin:DocumentAuthor>
        <fin:DocumentSecurityGroup>FAFusionImportExport</fin:DocumentSecurityGroup>
        <fin:DocumentAccount>fin$/generalLedger$/import$</fin:DocumentAccount>
        <fin:DocumentName>?</fin:DocumentName>
        <fin:DocumentId>?</fin:DocumentId>
      </typ:document>
    </typ:uploadFileToUcm>
  </soapenv:Body>
</soapenv:Envelope>

7 Comments

Thanks for your quick response. But this code is returning only #document when I am displaying the content using Write-host $xml.
try write-host $xml.OuterXml
@Shiv Shankar Maiti - sorry did not recognize that the only issue you have is to display it on the screen, I updated the code.
@Toni thanks for your response. My problem is not with display. My file containing SOAP format (xml). I have to add my data to that Content node where we have mentioned 'Hello World' and send the entire xml to SOAP API.
@Shiv Shankar Maiti - ok Thor already showed you one way how to change a value and save it to the xml... this works for you or?
|

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.