【发布时间】:2011-09-02 13:59:52
【问题描述】:
我在我的 Delphi 和 .Net C# 测试项目中使用 WebBrowser 控件导航到本地测试 XML 文件并尝试将内容保存回 .Net DocumentCompleted Event 和 Delphi @987654322 中的 XML 文件@事件。
问题是我总是得到 HTML,它会被浏览器转换以供查看(检查我的输出:我使用以下代码保存了它)
procedure TForm1.SaveHTMLSourceToFile(const FileName: string;
WB: TWebBrowser);
var
PersistStream: IPersistStreamInit;
FileStream: TFileStream;
Stream: IStream;
SaveResult: HRESULT;
begin
PersistStream := WB.Document as IPersistStreamInit;
FileStream := TFileStream.Create(FileName, fmCreate);
try
Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
SaveResult := PersistStream.Save(Stream, True);
if FAILED(SaveResult) then
MessageBox(Handle, 'Fail to save source', 'Error', 0);
finally
FileStream.Free;
end;
end;
嗯,我几乎尝试了所有方法,到处搜索,但直到现在都找不到任何有用的东西。使用以下 Delphi 代码,我设法显示了有效的 SOURCE(这意味着源在某处)但我不能使用它,因为它会播下一个对话框并且不容易获取数据并关闭该对话框(在我的测试用例中我得到了带有我的 xml 内容的 notepad.exe)
AWebBrowser.Document.QueryInterface(IOleCommandTarget, CmdTarget) ;
if CmdTarget <> nil then
try
CmdTarget.Exec(PtrGUID, HTMLID_VIEWSOURCE, 0, vaIn, vaOut) ;
finally
CmdTarget._Release;
end;
我还设法使用 xxx-HIDE-xxx 标志调用 SAVE AS 调用,但它会在 IE 5 上缝合,将显示另存为对话框(隐藏标志将被忽略)。
我还尝试从缓存(缓存 API)中获取 XML 数据,但在我的情况下,我什么也得不到,而且 2. 如果在客户机器上禁用缓存怎么办? ;-)
InnerText 或 InnerHTML atc。不能使用,因为它们包含 - 和 + 字符并且不代表原始 RAW 数据(源)
仅供参考:我无法使用 WebClient 或 Indy 组件来访问 xml。我也不能充当代理,因为在客户机器上打开端口(比如说 8080)的问题对于特权用户访问来说很痛苦。
所以我在这里问你是否知道如何解决我的问题?
提前致谢, 干杯
输入:
<?xml version="1.0" encoding="UTF-8"?>
<test><data>xxxx</data></test>
输出:
<HTML><HEAD>
<STYLE>BODY{font:x-small 'Verdana';margin-right:1.5em}
.c{cursor:hand}
.b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none}
.e{margin-left:1em;text-indent:-1em;margin-right:1em}
.k{margin-left:1em;text-indent:-1em;margin-right:1em}
.t{color:#990000}
.xt{color:#990099}
.ns{color:red}
.dt{color:green}
.m{color:blue}
.tx{font-weight:bold}
.db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier}
.di{font:small Courier}
.d{color:blue}
.pi{color:blue}
.cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888}
.ci{font:small Courier;color:#888888}
PRE{margin:0px;display:inline}</STYLE>
<SCRIPT><!--
function f(e){
if (e.className=="ci"){if (e.children(0).innerText.indexOf("\n")>0) fix(e,"cb");}
if (e.className=="di"){if (e.children(0).innerText.indexOf("\n")>0) fix(e,"db");}
e.id="";
}
function fix(e,cl){
e.className=cl;
e.style.display="block";
j=e.parentElement.children(0);
j.className="c";
k=j.children(0);
k.style.visibility="visible";
k.href="#";
}
function ch(e){
mark=e.children(0).children(0);
if (mark.innerText=="+"){
mark.innerText="-";
for (var i=1;i<e.children.length;i++)
e.children(i).style.display="block";
}
else if (mark.innerText=="-"){
mark.innerText="+";
for (var i=1;i<e.children.length;i++)
e.children(i).style.display="none";
}}
function ch2(e){
mark=e.children(0).children(0);
contents=e.children(1);
if (mark.innerText=="+"){
mark.innerText="-";
if (contents.className=="db"||contents.className=="cb")
contents.style.display="block";
else contents.style.display="inline";
}
else if (mark.innerText=="-"){
mark.innerText="+";
contents.style.display="none";
}}
function cl(){
e=window.event.srcElement;
if (e.className!="c"){e=e.parentElement;if (e.className!="c"){return;}}
e=e.parentElement;
if (e.className=="e") ch(e);
if (e.className=="k") ch2(e);
}
function ex(){}
function h(){window.status=" ";}
document.onclick=cl;
--></SCRIPT>
</HEAD>
<BODY class="st"><DIV class="e">
<SPAN class="b"> </SPAN>
<SPAN class="m"><?</SPAN><SPAN class="pi">xml version="1.0" encoding="UTF-8" </SPAN><SPAN class="m">?></SPAN>
</DIV>
<DIV class="e">
<DIV class="c" STYLE="margin-left:1em;text-indent:-2em"><A href="#" onclick="return false" onfocus="h()" class="b">-</A>
<SPAN class="m"><</SPAN><SPAN class="t">test</SPAN><SPAN class="m">></SPAN></DIV>
<DIV><DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b"> </SPAN>
<SPAN class="m"><</SPAN><SPAN class="t">data</SPAN><SPAN class="m">></SPAN><SPAN class="tx">xxxx</SPAN><SPAN class="m"></</SPAN><SPAN class="t">data</SPAN><SPAN class="m">></SPAN>
</DIV></DIV>
<DIV><SPAN class="b"> </SPAN>
<SPAN class="m"></</SPAN><SPAN class="t">test</SPAN><SPAN class="m">></SPAN></DIV>
</DIV></DIV>
</BODY>
</HTML>
【问题讨论】:
-
等等——为什么你不能直接用 WebClient 或 Indy 下载文件?看起来那些应该比涉及一个巨大的 UI 控件更简单。
-
嗯,这只是我在这里发布的一个示例。我编写了那个测试 xml/test 应用程序。真实场景非常复杂,需要用户在浏览器中进行交互,在用户完成所有操作后,浏览器和用户之间会发布一些帖子,直到最终结果是一个 XML 文件,您无法控制它的来源!这就是为什么!
标签: c# xml delphi browser webbrowser-control