Reputation: 5408
I am trying to loop through some XML and set the value of a string to be equal to a particular nodes contents. The XML looks like:
<RootNode>
<SubNode>test<SubNode>
<SubNode><ExtraMarkup>some value</ExtraMarkup><SubNode>
</RootNode>
Where each sub node can either contain a value or additional XML child nodes. For the first subnode this code works correctly:
for Node := 0 to RootNode.childNodes.length-1 do begin
AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text;
// More code here...
end;
The problem is when the subnode contains child nodes. I would like the value of AttrValue to be 'test
' or '<ExtraMarkup>some value</ExtraMarkup>
' as a string.
If instead of text I get the XML attribute the markup is not preserved.
Upvotes: 3
Views: 9290
Reputation: 5408
for Node := 0 to RootNode.childNodes.length-1 do begin
// Check if the Value stored in SubNode node is xml
if (RootNode.childNodes[Node].selectSingleNode('SubNode').hasChildNodes and
DealAttributesNode.childNodes[Node].selectSingleNode('SubNode').childNodes[0].hasChildNodes) then begin
AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').childNodes[0].Xml;
end
else begin
AttrValue := RootNode.childNodes[Node].selectSingleNode('SubNode').Text;
end;
end;
Upvotes: 0
Reputation: 243459
Instead of .Text
you need a property such as InnerText
or InnerXml
.
Upvotes: 1
Reputation: 121599
I think you probably want "IXmlNode.NodeValue".
Here's an example:
Function TGlobalConfig.GetXmlItem(CurNode : IXMLNODE; Section : String; var Value : String; Default : String) : Boolean;
var
ChildNode: IXMLNode;
begin
if Assigned(CurNode) then
begin
ChildNode := CurNode.ChildNodes.FindNode(Section);
if (ChildNode <> nil) then
if VarIsNull(ChildNode.NodeValue) then
Value := Default
else Value := ChildNode.NodeValue;
...
Upvotes: 1