How to change XML value

I have an xml document that I send as a soap request. The xml is in a constant. I would like to change one of the values in the xml before sending the request.
What is the best way to do that? I have looked at the examples but they don’t work with my xml file. See the xml below, I just want to change the value in itemnumber with a value from a text box.

<Envelope xmlns=""> <Body> <GetPriceAndAvailability xmlns=""> <!-- Optional --> <pRequest> <items xmlns=""> <!-- Optional --> <TVHWS.item_request> <id>1</id> <itemnumber>sy188</itemnumber> <quantity>1</quantity> <reference>myreference1</reference> <wh>1</wh> </TVHWS.item_request> </items> <passport xmlns=""> <accesskey>mykey!</accesskey> <account>myaccount</account> <password></password> <requestkey>myrequest</requestkey> <username></username> </passport> </pRequest> </GetPriceAndAvailability> </Body> </Envelope>

You will need to pull your constant into a string that can be modified before changing it, but I think that RegEx is going to be the easiest way to accomplish this.

[code]// sourceText is your XML
// replacementString would be the value that you want in itemnumber
// replacedText is your newly modified XML

dim rx as new RegEx
rx.SearchPattern = “(?mi-Us)(^)(.*)($)”
rx.ReplacementPattern = “\1” + replacementString + “\3”

dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
rxOptions.ReplaceAllMatches = true

dim replacedText as string = rx.Replace( sourceText )

Disclaimer: It is most certainly possible to create an invalid XML string with this method depending on what you pass in for the replacementString

I would skip the whole regex thing. If this is in a constant you know it’s not going to change, so a simple placeholder and String = Constant.Replace(Placeholder, Value) would suffice.

Just an alternative :slight_smile:

Ahhh yes, you are definitely right Tim.
I even commented about the constant, but completely glossed over how simple that makes the solution. >.<


Or you could break the constant into two constants, the first before the item number and the second after and just build your xml string from there.

You could also use XQL to get the itemnumber node and change its value.

Or load the constant into an xml document and update it there.

[code]Public Function SetItemNumber(ItemNumber As String) as String
Dim xDoc As New XmlDocument
xDoc.LoadXml(xmlstring) ’ xmlstring is the constant
Dim xItemRequest As XmlNode = xDoc.FirstChild ’ Envelope
xItemRequest = xItemRequest.FirstChild ’ Body
xItemRequest = xItemRequest.FirstChild ’ GetPriceAndAvailability
xItemRequest = xItemRequest.LastChild ’ pRequest
xItemRequest = xItemRequest.FirstChild ’ pRequest
xItemRequest = xItemRequest.LastChild ’ TVHWS.item_request

For i As Integer = 0 To xItemRequest.ChildCount - 1
Dim xNode As XmlNode = xItemRequest.Child(i)
If xNode.Name = “itemnumber” Then
xNode.FirstChild.Value = ItemNumber
Exit For
End If

Return xDoc.ToString
End Function

Thank you for all the great suggestions. I went with Tim’s answer as it was the easiest to implement into my code and it is now working.