Im trying to add a new XML Node to a existing XML which is a API Response. But unfortunately i cannot get it to work trying many tutorials and examples. I cannot tell you which Options i tried but the last one is included...
I have this XML (Which is a API Response):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccessPermissions xmlns="http://localhost/RESTApi/v1">
<AccessProfilePermissions/>
<ReaderSpecialPermissions/>
<RoomZoneSpecialPermissions>
<RoomZoneSpecialPermission>
<RoomZoneNumber>6</RoomZoneNumber>
<AccessWeeklyProfileNumber>1</AccessWeeklyProfileNumber>
<ValidFrom>2023-07-13</ValidFrom>
</RoomZoneSpecialPermission>
</AccessPermissions>
Now i want to add another <RoomZoneSpecialPermission>
into the XML to send it back to the API Endpoint. I expect something like that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccessPermissions xmlns="http://localhost/RESTApi/v1">
<AccessProfilePermissions/>
<ReaderSpecialPermissions/>
<RoomZoneSpecialPermissions>
<RoomZoneSpecialPermission>
<RoomZoneNumber>6</RoomZoneNumber>
<AccessWeeklyProfileNumber>1</AccessWeeklyProfileNumber>
<ValidFrom>2023-07-13</ValidFrom>
</RoomZoneSpecialPermission>
<RoomZoneSpecialPermission>
<RoomZoneNumber>7</RoomZoneNumber>
<AccessWeeklyProfileNumber>1</AccessWeeklyProfileNumber>
<ValidFrom>2023-07-31</ValidFrom>
</RoomZoneSpecialPermission>
</AccessPermissions>
My Script (Without API Call, since that works perfectly fine):
# API Response to XML Object
$xmlObject = [xml]$apiresponse
# New Permission in XML Format and conver to XML Object
$newxml = "<RoomZoneSpecialPermission><RoomZoneNumber>7</RoomZoneNumber><AccessWeeklyProfileNumber>1</AccessWeeklyProfileNumber><ValidFrom>2023-07-31</ValidFrom></RoomZoneSpecialPermission>"
$newPermissionObject = [xml]$newxml
# Select RoomZoneSpecialPermissions Node
$roomZoneSpecialPermissions = $xmlObject.SelectSingleNode("//a:RoomZoneSpecialPermissions", $xmlObject.DocumentElement.NamespaceURI)
# New RoomZoneSpecialPermission Node
$newPermissionElement = $xmlObject.CreateElement("RoomZoneSpecialPermission", "http://localhost/RESTApi/v1")
# Add Elements to RoomZoneSpecialPermission Node generated above
$newPermissionElement.AppendChild($xmlObject.CreateElement("RoomZoneNumber", "http://localhost/RESTApi/v1")).InnerText = "7"
$newPermissionElement.AppendChild($xmlObject.CreateElement("AccessWeeklyProfileNumber", "http://localhost/RESTApi/v1")).InnerText = "1"
$newPermissionElement.AppendChild($xmlObject.CreateElement("ValidFrom", "http://localhost/RESTApi/v1")).InnerText = "2023-07-31"
$roomZoneSpecialPermissions.Trust.FrameworkPolicy.ClaimsProviders.AppendChild($newPermissionElement)
$modifiedxml = $xmlObject.OuterXml
Write-Output $modifiedxml
With the many tutoritals i tried came different Errors like:
Exception callind "AppendChild" with "1" arguments "The node to be inserted is from a different document context"
Cannot find an overload for "SelectSingleNode" and the argument count 2
Could you please tell me how to accomplish this task? Im pretty devastated right now since none of the explanations worked so far.