Hallo, I use TNativeXML v4.09 to create XML like this :
procedure TForm1.Button1Click(Sender: TObject);
var aaa: TNativeXml;
vSectionName : string;
begin
vSectionName := 'word1 word2';
aaa:= TNativeXml.Create(Self);
aaa.CreateName('Root');
aaa.Root.NodeNew(vSectionName);
aaa.Root.NodeByName('word1 word2').Value:='this is value';
aaa.XmlFormat := xfReadable;
aaa.SaveToFile('test.xml');
end;
and then the XML file result :
<Root> <word1 word2="">this is value</word1> </Root>Its is look normal, but when I want to get the section with name as vSectionName like this :
procedure TForm1.Button2Click(Sender: TObject);
var aaa : TNativeXml;
vSectionName : string;
vNode : TXmlNode;
begin
vSectionName := 'word1 word2';
try
aaa := TNativeXml.Create(Self);
aaa.LoadFromFile('test.xml');
vNode := aaa.Root.NodeByName(vSectionName);
if vNode=nil then
ShowMessage('Section not found')
else
ShowMessage('Section found');
finally
FreeAndNil(aaa);
end;
end;
I always get message : 'Section not found'.
Then to track this error with this in NodeByName function:
function TXmlNode.NodeByName(const AName: Utf8String): TXmlNode;
var
i: integer;
vF : boolean;
begin
result := nil;
for i := 0 to GetNodeCount - 1 do begin
vF := (Utf8CompareText(GetNodes(i).Name, AName) = 0);
if vF then
Application.MessageBox(PWideChar('TXmlNode.NodeByName-> Found AName='+AName+'|GetNodes('+IntToStr(i)+').Name='+GetNodes(i).Name+'|GetNodes('+IntToStr(i)+').NameUnicode='+GetNodes(i).NameUnicode),'TXmlNode.NodeByName',0)
else
Application.MessageBox(PWideChar('TXmlNode.NodeByName-> Not Found AName='+AName+'|GetNodes('+IntToStr(i)+').Name='+GetNodes(i).Name+'|GetNodes('+IntToStr(i)+').NameUnicode='+GetNodes(i).NameUnicode),'TXmlNode.NodeByName',0);
//if (Utf8CompareText(GetNodes(i).Name, AName) = 0) then
if vF then
begin
Result := GetNodes(i);
exit;
end;
end;
end;
I got the section name with multiple word name ("word1 word2") change to single word name ("word1") when we load from file XML.
Why this happen?
Thanks in advance.
Sorry the XML file like this :
And code above :
and in NativeXML.TXMLNode.NodeByName function :