Upps, dachte ich hätte schon geantwortet.
Die EAccessViolation konnte ich nicht nachvollziehen.
> himxml_164 und XE3
> heute Abend kann ich nochmal schnell XE und 10.1 testen
Wie sieht dein System denn aus?
> Versionen
XML und Delphi
Hab den Test nochmal schnell zusammengeklöppelt
Delphi-Quellcode:
var XML: TXMLFile;
Node: TXMLNode;
S:
String;
begin
// EAccessViolation
XML := TXMLFile.Create;
try
Node :=
XML.RootNode.AddNode('
test');
XML.RootNode.Nodes.Delete(Node);
XML.SaveToFile('
test.xml');
// <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
// <xml/>
finally
XML.Free;
end;
// Node "&amp;" statt "&"
XML := TXMLFile.Create;
try
XML.RootNode.AddNode('
test').Text := '
&';
XML.SaveToFile('
test.xml');
S := TFile.ReadAllText('
test.xml');
OutputDebugString(PChar(S));
//ShowMessage(S);
// <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
// <xml>
// <test>&</test>
// </xml>
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
S := '
"' +
XML.RootNode.Node['
test'].Text + '
"' + sLineBreak
+ '
"' +
XML.RootNode.Node['
test'].Text_S + '
"' + sLineBreak
+ '
"' +
XML.RootNode.Node['
test'].Text_D + '
"';
OutputDebugString(PChar(S));
//ShowMessage(S);
// "&"
// "&"
// "&"
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
XML.SaveToFile('
test.xml');
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
S := '
"' +
XML.RootNode.Node['
test'].Text + '
"' + sLineBreak
+ '
"' +
XML.RootNode.Node['
test'].Text_S + '
"' + sLineBreak
+ '
"' +
XML.RootNode.Node['
test'].Text_D + '
"';
OutputDebugString(PChar(S));
//ShowMessage(S);
// "&"
// "&"
// "&"
finally
XML.Free;
end;
// Attribute "&amp;" statt "&"
XML := TXMLFile.Create;
try
XML.RootNode.AddNode('
test').Attributes.Add('
attr', '
&');
XML.SaveToFile('
test.xml');
S := TFile.ReadAllText('
test.xml');
OutputDebugString(PChar(S));
//ShowMessage(S);
// <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
// <xml>
// <test attr="&"/>
// </xml>
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
S := '
"' +
XML.RootNode.Node['
test'].Attribute['
attr'] + '
"';
OutputDebugString(PChar(S));
//ShowMessage(S);
// "&"
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
XML.SaveToFile('
test.xml');
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
S := '
"' +
XML.RootNode.Node['
test'].Attribute['
attr'] + '
"';
OutputDebugString(PChar(S));
//ShowMessage(S);
// "&"
finally
XML.Free;
end;
// DateTime
XML := TXMLFile.Create;
try
XML.RootNode.AddNode('
test').Text := Now;
XML.RootNode.AddNode('
test').Attributes.Add('
attr', Now);
XML.SaveToFile('
test.xml');
S := TFile.ReadAllText('
test.xml');
OutputDebugString(PChar(S));
//ShowMessage(S);
// <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
// <xml>
// <test>2018-02-19T10-05-51.276</test>
// <test attr="2018-02-19T10-05-51.598"/>
// </xml>
finally
XML.Free;
end;
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
S := '
"' + DateTimeToStr(
XML.RootNode.Node['
test'].Text) + '
"' + sLineBreak;
//S := S + '"' + XML.RootNode.Node['test'].Text + '"' + sLineBreak; // EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
S := S + '
"' +
XML.RootNode.Node['
test'].Text_S + '
"' + sLineBreak + sLineBreak;
S := S + '
"' + DateTimeToStr(
XML.RootNode.Node['
test'].Attribute['
attr']) + '
"'+ sLineBreak;
S := S + '
"' +
XML.RootNode.Node['
test'].Attribute['
attr'] + '
"' + sLineBreak + sLineBreak;
S := S + '
"' + TXHelper.DateTimeToXML(Now, 3) + '
"';
OutputDebugString(PChar(S));
//ShowMessage(S);
// "19.02.2018 10:14:41"
// "2018-02-19T10-14-41.622"
//
// "30.12.1899"
// ""
//
// "2018-02-19T10-14-48"
finally
XML.Free;
end;
So ging alles.
Diesmal auch das Datum probiert und ...
Zitat:
EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
Bug im XE3?
> Der Cast ist ja wohl andersrum, also Variant(DateTime) > String
Und bei den Attributen klappt das Auslesen nicht ... muß aber noch schauen warum.
Mir fiel grade ein warum es nicht ging.
> Attribut im zweiten Node, aber den ersten Node ausgelesen
Delphi-Quellcode:
// richtig
XML := TXMLFile.Create;
try
XML.LoadFromFile('
test.xml');
S := '
"' + DateTimeToStr(
XML.RootNode.Node['
test'].Text) + '
"' + sLineBreak;
//S := S + '"' + XML.RootNode.Node['test'].Text + '"' + sLineBreak; // EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
S := S + '
"' +
XML.RootNode.Node['
test'].Text_S + '
"' + sLineBreak + sLineBreak;
S := S + '
"' + DateTimeToStr(
XML.RootNode.Node['
test'].Attribute['
attr']) + '
"'+ sLineBreak;
S := S + '
"' +
XML.RootNode.Node['
test'].Attribute['
attr'] + '
"' + sLineBreak + sLineBreak;
S := S + '
"' + DateTimeToStr(
XML.RootNode.Nodes[1].Attribute['
attr']) + '
"'+ sLineBreak;
//S := S + '"' + XML.RootNode.Nodes[1].Attribute['attr'] + '"' + sLineBreak + sLineBreak; // EVariantTypeCastError: Could not convert variant of type (UnicodeString) into type (Date)
S := S + sLineBreak;
//S := S + '"' + DateTimeToStr((XML.RootNode.XPath['test[2]'] as TXMLNode).Attribute['attr']) + '"'+ sLineBreak;
//S := S + '"' + (XML.RootNode.XPath['test[2]'] as TXMLNode).Attribute['attr'] + '"' + sLineBreak + sLineBreak;
S := S + '
"' + TXHelper.DateTimeToXML(Now, 3) + '
"';
OutputDebugString(PChar(S));
//ShowMessage(S);
// "19.02.2018 10:28:40"
// "2018-02-19T10-28-40.633"
//
// "30.12.1899"
// ""
//
// "19.02.2018 10:28:40"
//
// "2018-02-19T10-28-45"
finally
XML.Free;
end;