Goh*_*ool 9 c# xml browser delphi webbrowser-control
我在我的Delphi和.Net C#测试项目中使用WebBrowser控件导航到本地测试XML文件,并尝试将内容保存回.Net DocumentCompleted事件和Delphi onNavigateComple2事件中的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;
Run Code Online (Sandbox Code Playgroud)
好吧,我已经尝试了几乎所有东西,到处搜索,但到目前为止找不到任何有用的东西.使用下面的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;
Run Code Online (Sandbox Code Playgroud)
我也设法调用SAVE AS呼叫与XXX-HIDE-XXX标志,但它接缝了IE 5另存为对话框将显示(Hide标志将被忽略).
我还试图从缓存中获取XML数据(缓存API)但在我的情况下我不会得到任何东西2.如果在客户机器上缓存被禁用怎么办?;-)
InnerText或InnerHTML atc.不能使用,因为它们包含 - 和+ char并且不代表原始RAW数据(SOURCE)
仅供参考:我无法使用WebClient或Indy组件访问xml.我也无法作为代理播放,因为在客户机器上打开端口(比如说8080)的问题是特权用户访问的痛苦.
所以我在这里问你是否知道如何解决我的问题?
提前谢谢,干杯
输入:
<?xml version="1.0" encoding="UTF-8"?>
<test><data>xxxx</data></test>
Run Code Online (Sandbox Code Playgroud)
输出:
<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>
Run Code Online (Sandbox Code Playgroud)
我认为你处理这个问题的方式是错误的。控件TWebBrowser是用于查看的视觉控件。您也许能够从中提取底层数据,但从根本上来说,使用视觉控制来下载某些内容(非视觉操作)并不是一个好方法。相反,您应该使用专用 API 下载该文件。
仅供参考:我无法使用 WebClient 或 Indy 组件来访问 xml。我也不能玩代理,因为......
你没有这些组件吗?在这种情况下,我建议您使用以下方法之一:
TDownloadURL是一个内置类,对于简单下载文件很有用。使用它的一些例子:
互联网读取文件。这是我个人在自己的代码中使用的 - 我有一个小线程类来异步下载文件并在完成时通知主线程,使用此函数实现。通过以下方式使用它:
INTERNET_FLAG_HYPERLINK or INTERNET_FLAG_NO_UI标志来获取另一个句柄抱歉,我无法发布源代码,但它们是简单的函数,您应该会发现它很容易编写。
这些方法将获得一个文件或一个缓冲区,每个文件或缓冲区都包含 XML 文件的原始内容。
编辑:我看到你解释了一些关于为什么不能使用 Indy 的原因:
“真实的场景非常复杂,需要用户在浏览器中进行交互,在用户完成所有操作后,浏览器和用户之间会发布一些帖子,直到最终结果是一个 XML 文件,您无法控制该文件的来源!”
我不确定这会阻止您使用 Indy:相反,您只需要获取此 XML 的位置即可。事实上你无法控制它在哪里并不重要,你只需要找出它在哪里。如果您只有一个链接,则可以抓取 HTML(您已经可以从浏览器获取 HTML - 事实上,这就是您的问题!),或者查看文档TWebBrowser所在的最终位置,然后下载该文件。换句话说,让用户做任何他们必须做的事情来导航到最终的 XML 文件,而不是尝试从 Web 浏览器控件中提取它,而是自己下载它。
| 归档时间: |
|
| 查看次数: |
2962 次 |
| 最近记录: |