Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我序列化为字符串时,为什么TXmlDocument会省略编码?_Xml_Delphi_Encoding - Fatal编程技术网

当我序列化为字符串时,为什么TXmlDocument会省略编码?

当我序列化为字符串时,为什么TXmlDocument会省略编码?,xml,delphi,encoding,Xml,Delphi,Encoding,如果我从一行到另一行构建XML,我可以设置编码,但是当我从文件加载它时,我不能添加编码。见: procedure TForm1.Button1Click(Sender: TObject); var x : TXMLDocument; s : string; w : WIdeString; begin s := '<?xml version="1.0"?><a><b/></a>'; x := TXMLDocum

如果我从一行到另一行构建XML,我可以设置编码,但是当我从文件加载它时,我不能添加编码。见:

procedure TForm1.Button1Click(Sender: TObject);
var
    x : TXMLDocument;
    s : string;
    w : WIdeString;
begin
    s := '<?xml version="1.0"?><a><b/></a>';
    x := TXMLDocument.Create(Self);
    x.XML.Text := s;
    x.Active := True;
    x.Encoding := 'UTF-8';
    x.DocumentElement.childNodes['b'].attributes['x'] := '1';
    x.SaveToXML(w);
    ShowMessage(w);
end;
有趣的是,结果中缺少编码部分


如何使结果包含XML编码?

您正在将XML保存到一个宽字符串。根据定义,A是UTF16编码的,因此指定另一种编码没有任何意义。IIRC,您可以指定一个编码=UTF-16,它不会被删除,因为这就是它


但是,如果因此将XML文档保存到流中,则可以指定不同的编码。

将XML保存到宽字符串中。根据定义,A是UTF16编码的,因此指定另一种编码没有任何意义。IIRC,您可以指定一个编码=UTF-16,它不会被删除,因为这就是它


但是,如果因此将XML文档保存到流中,则可以指定不同的编码。

请注意,不要以这种方式实例化TXMLDocument。使用该函数来实例化IXMLDocument。请注意,不要以这种方式实例化TXMLDocument。改为使用函数实例化IXMLDocument。