如何识别文件是否具有XML格式? [英] How to recognize whether file has XML format or not?

查看:88
本文介绍了如何识别文件是否具有XML格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何识别文件是否具有XML格式?

以下是代码段:


XmlDocument * pDomDocument = new XmlDocument();

尝试

{

pDomDocument->加载(strFileName);

}

catch(例外* e)

{

....

}


Of当然,如果我们尝试加载非XML文件,则会抛出异常。

但是在其他情况下也可能抛出异常(文件不存在

等)

我需要在尝试加载非XML文件时识别具体情况。

对我来说似乎XmlException类没有提供这种细节。 />
任何其他想法如何实现同一目标?提前致谢

How to recognize whether file has XML format or not?
Here is the code segment:

XmlDocument* pDomDocument = new XmlDocument();
try
{
pDomDocument->Load(strFileName ) ;
}
catch(Exception* e)
{
....
}

Of course if we try to load non XML file, the exception will be thrown.
However exception might be thrown in other cases as well (File doesn''t exist
etc)
I need identify the specific case when attempt was made to load non XML file.
Seems to me XmlException class doesn''t provide that kind of details.
Any other ideas how can I accomplish the same goal? Thanks in advance

推荐答案

嗨Dale


这就是为什么不止一个框架中的异常类。 VB代码

如下,但你明白了:


HTH


Nigel

尝试

Dim t As New Xml.XmlTextReader(" C:\ test.xml")

Dim d As New Xml.XmlDocument

d.Load(t)

MessageBox.Show(d.OuterXml)

Catch ex As Xml.XmlException

MessageBox.Show(" XML exception"& ex.Message)


Catch ex As IO.FileNotFoundException

MessageBox.Show(" File Not发现异常& ex.Message


结束尝试


" Dale"写道:
Hi Dale

That''s why there''s more than one Exception class in the Framework. VB code
follows but you get the idea:

HTH

Nigel

Try
Dim t As New Xml.XmlTextReader("C:\test.xml")
Dim d As New Xml.XmlDocument
d.Load(t)
MessageBox.Show(d.OuterXml)
Catch ex As Xml.XmlException
MessageBox.Show("XML exception " & ex.Message)

Catch ex As IO.FileNotFoundException
MessageBox.Show("File Not found exception " & ex.Message)

End Try

"Dale" wrote:
如何识别文件是否具有XML格式?
以下是代码段:

XmlDocument * pDomDocument = new XmlDocument( );
尝试
{
pDomDocument->加载(strFileName);
}
catch(例外* e)
{
。 ..
}

当然如果我们尝试加载非XML文件,则会抛出异常。
然而,在其他情况下也可能抛出异常(File doesn'存在
等等。
我需要在尝试加载非XML文件时识别具体情况。
对我来说似乎XmlException类没有提供那种细节。 />任何其他想法如何实现相同的目标?提前致谢
How to recognize whether file has XML format or not?
Here is the code segment:

XmlDocument* pDomDocument = new XmlDocument();
try
{
pDomDocument->Load(strFileName ) ;
}
catch(Exception* e)
{
...
}

Of course if we try to load non XML file, the exception will be thrown.
However exception might be thrown in other cases as well (File doesn''t exist
etc)
I need identify the specific case when attempt was made to load non XML file.
Seems to me XmlException class doesn''t provide that kind of details.
Any other ideas how can I accomplish the same goal? Thanks in advance



Hello Nigel,


感谢您的回复。

我使用过文件不存在案例就是一个例子。如果文件确实存在,那么
存在但XML格式已损坏等等。我的观点是XmlException

在我们尝试加载非XML时没有提供明确的指示

档案。我错了吗?还有其他方法可以识别非XML文件吗?


谢谢,

Dale


" Nigel Armstrong"写道:
Hello Nigel,

Thank you for your response.

Well I used "File doesn''t exist" case just as example. What if file does
exists but with corrupted XML format etc. My point was the XmlException
doesn''t provide clear indication for the case when we try to load non XML
file. Am I wrong? Is there other ways to recognize non XML file?

Thanks,
Dale

"Nigel Armstrong" wrote:
嗨Dale

这就是为什么框架中有多个Exception类的原因。 VB代码
如下,但你明白了:

HTH

Nigel

尝试
Dim t As New Xml。 XmlTextReader(" C:\ test.xml")
Dim d As New Xml.XmlDocument
d.Load(t)
MessageBox.Show(d.OuterXml)
Catch ex As Xml.XmlException
MessageBox.Show(" XML exception"& ex.Message)

Catch ex As IO.FileNotFoundException
MessageBox.Show("文件未找到例外& ex.Message

结束尝试

Dale写道:
Hi Dale

That''s why there''s more than one Exception class in the Framework. VB code
follows but you get the idea:

HTH

Nigel

Try
Dim t As New Xml.XmlTextReader("C:\test.xml")
Dim d As New Xml.XmlDocument
d.Load(t)
MessageBox.Show(d.OuterXml)
Catch ex As Xml.XmlException
MessageBox.Show("XML exception " & ex.Message)

Catch ex As IO.FileNotFoundException
MessageBox.Show("File Not found exception " & ex.Message)

End Try

"Dale" wrote:
如何识别文件是否具有XML格式?
以下是代码段:

XmlDocument * pDomDocument = new XmlDocument( );
尝试
{
pDomDocument->加载(strFileName);
}
catch(例外* e)
{
。 ..
}

当然如果我们尝试加载非XML文件,则会抛出异常。
然而,在其他情况下也可能抛出异常(File doesn'存在
等等。
我需要在尝试加载非XML文件时识别具体情况。
对我来说似乎XmlException类没有提供那种细节。 />任何其他想法如何实现相同的目标?提前致谢
How to recognize whether file has XML format or not?
Here is the code segment:

XmlDocument* pDomDocument = new XmlDocument();
try
{
pDomDocument->Load(strFileName ) ;
}
catch(Exception* e)
{
...
}

Of course if we try to load non XML file, the exception will be thrown.
However exception might be thrown in other cases as well (File doesn''t exist
etc)
I need identify the specific case when attempt was made to load non XML file.
Seems to me XmlException class doesn''t provide that kind of details.
Any other ideas how can I accomplish the same goal? Thanks in advance



(Hi Nigel!)


戴尔,


问题是解析器无法确定您是否有非XML

文件或格式错误的XML文件。不知道XML格式是否已损坏。

要么文件格式正确,要么是XML,要么不是,而且它不是

XML。我没有看到任何其他选择。解析器看到非XML文件

无论如何都是格式错误的XML文件。当您将文件提交到XML

解析器时,它应该是XML。如果不是,那么解析器只会报告格式错误。你不能指望一个XML解析器报告抱歉,

它是一个位图! :-)。


-

Patrick Philippot - 微软MVP

MainSoft咨询服务
www.mainsoft.fr
(Hi Nigel!)

Dale,

The problem is that the parser can''t decide whether you have a non XML
file or a malformed XML file. There''s no idea of corrupted XML format.
Either the file is well-formed and it''s XML or it is not and it''s not
XML. I don''t see any alternative. A non XML file is seen by the parser
as a malformed XML file anyway. When you submit the file to the XML
parser, it''s supposed to be XML. If it''s not, the parser will just
report "malformed". You can''t expect an XML parser to report "Sorry,
it''s a bitmap!" :-) .

--
Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr


这篇关于如何识别文件是否具有XML格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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