XmlNode with attribute and value

Hi. I’ve stuck in very simple at glance structure. I’m trying to get xml code like:

<InstdAmt Ccy="EUR">0.12</InstdAmt>
  Dim xml as new XmlDocument
  Dim attr as XMLAttribute
  
  // <InstdAmt Ccy="EUR">0.12</InstdAmt>
  dim node as XmlTextNode = xml.CreateTextNode("InstdAmt")
  node.value = "0.12"
  
  attr=xml.CreateAttribute("Ccy")
  attr.value = "EUR"
  node.SetAttributeNode(attr)
  xml.AppendChild node

in this case I’m getting exception from string “node.SetAttributeNode(attr)” with text “msg:DOM exception 16: invalid access”
Looks like it is not possible to add attribute to XmlTextNode

Another attempt:

  Dim xml as new XmlDocument
  Dim attr as XMLAttribute
  
  // <InstdAmt Ccy="EUR">0.12</InstdAmt>
  dim node as XmlElement = xml.CreateElement("InstdAmt")
  node.value = "0.12"
  
  attr=xml.CreateAttribute("Ccy")
  attr.value = "EUR"
  node.SetAttributeNode(attr)
  xml.AppendChild node  

On line "node.value=“0.12"” I’m getting “msg:DOM exception 7: modification not allowed”. In the xojo wiki doc there is info about XmlElement.Value - "Not supported by all XMLNode subclasses. "

Looks like both versions are mutually exclusive.

Is there a third way? How to get xml node with attribute and value?

Thank you!

The trick is this: text nodes are one level lower than you think. You should first create an XmlElement, and assign it the attribute Ccy=“EUR”. Then create an XmlTextNode as a child of that element, and give it the value of “0.12”.

Thank you very much! It works!

Hi Kyryl Pekarov !

I’m doing an xml to send debit receipts to the bank. (Know Core 19).
Could I see the code you did to generate the xml? I’m blocked.

Could we reach an agreement?

Try this

  dim xml as new XmlDocument
  dim node as XmlNode
  dim attr as XmlAttribute
  dim text as XmlTextNode
  
  node = xml.CreateElement("InstAmt")
  text = xml.CreateTextNode("0.12")
  
  node.AppendChild(text)
  node.SetAttribute("Ccy", "EUR")
  
  xml.AppendChild(node)
  
  dim s as string = node.ToString   // <InstAmt Ccy="EUR">0.12</InstAmt>