0

I'm trying to insert an XML here-string into a XMLdocument. However, the saved XMLdoc, shows: "System.Xml.XmlDocument", not the content. How can I fix this?

[xml] $Doc = New-Object System.Xml.XmlDocument

$updateNode = [xml] "<Update>                       
                          <Request>Test</Request>
                     </Update>"

#Create XML declaration
$declaration = $Doc.CreateXmlDeclaration("1.0","UTF-8",$null)

#Append XML declaration
$Doc.AppendChild($declaration)
    
#Create root element
$root = $Doc.CreateNode("element","BrowseDetailsRequest",$null)

#Create node based on Here-String
$node = $Doc.CreateElement("element",$updateNode,$null)

#Append node
$root.AppendChild($node)
    
#Append root element
$Doc.AppendChild($root)   

Output at this moment:

<?xml version="1.0" encoding="UTF-8"?>
<BrowseDetailsRequest>
  <System.Xml.XmlDocument />
</BrowseDetailsRequest>
1
  • The problem is "CreateElement()" does not accept XML data type (2nd parameter). The 2nd parameter is string localName, which is the local name of the new element. Look at the signature of CreateElement() public virtual System.Xml.XmlElement CreateElement (string? prefix, string localName, string? namespaceURI); learn.microsoft.com/en-us/dotnet/api/… Commented Jul 20, 2021 at 8:07

1 Answer 1

2

You don't really manipulate the text in the xml. Use the objects to manipulate the xml. So you need to create an element for update and request and then assign the innertext value of request.

$Doc = New-Object System.Xml.XmlDocument
$declaration = $Doc.CreateXmlDeclaration("1.0","UTF-8",$null)
$Doc.AppendChild($declaration)
$root = $Doc.CreateNode("element","BrowseDetailsRequest",$null)
$elUpdate = $doc.CreateElement("element","Update",$null)
$elRequest = $doc.CreateElement("element","Request",$null)
$elRequest.InnerText = "Test"
$elUpdate.AppendChild($elRequest)
$root.AppendChild($elUpdate)
$doc.AppendChild($root)
Sign up to request clarification or add additional context in comments.

1 Comment

This is so horribly clumsy. Isn't there any other way?

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.