如何使用C#查找XML文件的大小 [英] How to find the size of XML file using C#

查看:189
本文介绍了如何使用C#查找XML文件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在将文件下载到本地桌面之前找到xml的大小,然后单击浏览器中的按钮,应该在弹出窗口或标签中显示这个大小的xml文件,只是为了向用户显示大小使用asp .net c#。我怎么能这样做



我尝试过的事情:



我我是xml概念的新手可以任何人帮我解决

I need to find the size of xml before downloading the file on to the local desktop after clicking the button in the browser this size of the xml file should be dispalyed in pop up or in a label just for displaying the size to user using asp .net c#. How can i do this

What I have tried:

I am new to xml concept can anybody help me out

推荐答案

问题是这是一个浏览器功能,而不是C#可以做的任何事情 - 除非文件是从您的网站在这种情况下是微不足道的:获取FileInfo,并查看Length属性:

The problem is that is a browser function, not anything that C# can do - unless the file is being downloaded from your website in which case it's trivial: Get the FileInfo, and look at the Length property:
long length = new System.IO.FileInfo(path).Length;





但是在浏览器中?您无法控制用户正在查看的内容,也无法提前检查其大小。如果你编写一个浏览器扩展,你可能会这样做,但这不会在C#中 - 而且你必须为每个浏览器编写一个新的扩展(并希望客户端从预先升级根本不支持扩展的周年纪念更新边缘



But in a browser? You can't control what the user is looking at, and you can't check in advance what size it's going to be. It's possible that if you write a browser extension you may be able to do this, but that won't be in C# - and you'd have to write a new extension of each browser (and hope that the client upgraded from the pre-Anniversary Update Edge which didn't support extensions at all)


如果您从网上下载它,您可以使用HEAD而不是获取XML文件Content-Length作为一个http标头。如果您使用webrequest进行提取,则以下或类似方法应该有效。这不会发送XML文件,只会发送响应头。







long len = 0 ;

WebRequest req = WebRequest.Create(url);

req.Method =HEAD;

using(WebResponse res = req。 GetResponse()){



len = res.ContentLength;



}
If you're downloading it from the web, you can use HEAD instead of get to get the XML files Content-Length as one of the http headers. The following, or similar, should work if you're using webrequest to fetch. This won't send the XML file, just the response headers.



long len=0;
WebRequest req = WebRequest.Create (url);
req.Method = "HEAD";
using(WebResponse res=req.GetResponse()) {

len=res.ContentLength;

}


这篇关于如何使用C#查找XML文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆