C# 在使用 XDocument.Load() 加载文件之前,如何测试文件以查看它是否是有效的 XML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/375590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one test a file to see if it's a valid XML file before loading it with XDocument.Load()?
提问by adeena
I'm loading an XML document in my C# application with the following:
我正在使用以下内容在我的 C# 应用程序中加载一个 XML 文档:
XDocument xd1 = new XDocument();
xd1 = XDocument.Load(myfile);
but before that, I do test to make sure the file exists with:
但在此之前,我会进行测试以确保该文件存在:
File.Exists(myfile);
But... is there an (easy) way to test the file before the XDocument.Load() to make sure it's a valid XML file? In other words, my user can accidentally click on a different file in the file browser and trying to load, say, a .php file causes an exception.
但是......是否有一种(简单的)方法可以在 XDocument.Load() 之前测试文件以确保它是一个有效的 XML 文件?换句话说,我的用户可能会不小心点击文件浏览器中的不同文件并尝试加载,比如说,一个 .php 文件会导致异常。
The only way I can think of is to load it into a StreamWriter and simple do a text search on the first few characters to make sure they say "
我能想到的唯一方法是将它加载到 StreamWriter 中,然后简单地对前几个字符进行文本搜索,以确保它们说“
Thanks!
谢谢!
-Adeena
-阿迪娜
采纳答案by Jennifer
It's probably just worth catching the specific exception if you want to show a message to the user:
如果您想向用户显示一条消息,那么捕获特定异常可能是值得的:
try
{
XDocument xd1 = new XDocument();
xd1 = XDocument.Load(myfile);
}
catch (XmlException exception)
{
ShowMessage("Your XML was probably bad...");
}
回答by Joel Coehoorn
Just load it and catch the exception. Same for File.Exists()
- the file system is volatileso just because File.Exists()
returns true doesn't mean you'll be able to open it.
只需加载它并捕获异常。同样的File.Exists()
- 文件系统是易变的,所以仅仅因为File.Exists()
返回 true 并不意味着您将能够打开它。
回答by Colby Africa
If you have an XSD for the XML, try this:
如果您有 XML 的 XSD,请尝试以下操作:
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class ValidXSD
{
public static void Main()
{
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any warnings or errors.
private static void ValidationCallBack (object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message);
else
Console.WriteLine("\tValidation error: " + args.Message);
}
}
Reference is here:
参考在这里:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.validationeventhandler.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.validationeventhandler.aspx
回答by Dimitre Novatchev
This question confuses "well-formed" with "valid" XML document.
A valid xml document is by definition a well formed document. Additionally, it must satisfy a DTDor a schema (an xml schema, a relaxng schema, schematronor other constraints) to be valid.
根据定义,有效的 xml 文档是格式良好的文档。此外,它必须满足DTD或模式(xml 模式、relaxng 模式、schematron或其他约束)才有效。
Judging from the wording of the question, most probably it asks:
从问题的措辞来看,很可能它会问:
"How to make sure a file contains a well-formed XML document?".
“如何确保文件包含格式良好的 XML 文档?”。
The answer is that an XML document is well-formed if it can be parsed successfully by a compliant XML parser. As the XDocument.Load()method does exactly this, you only need to catch the exception and then conclude that the text contained in the file is not well formed.
答案是,如果 XML 文档可以被兼容的 XML 解析器成功解析,那么它就是格式良好的。由于XDocument.Load()方法正是这样做的,因此您只需捕获异常,然后得出文件中包含的文本格式不正确的结论。
回答by BenAlabaster
As has previously been mentioned "valid xml" is tested by XmlDocument.Load(). Just catch the exception. If you need further validation to test that it's valid against a schema, then this does what you're after:
正如前面提到的,“有效的 xml”是由 XmlDocument.Load() 测试的。只需捕获异常。如果您需要进一步验证以测试它是否对架构有效,那么这将执行您的操作:
using System.Xml;
using System.Xml.Schema;
using System.IO;
static class Program
{
private static bool _Valid = true; //Until we find otherwise
private static void Invalidated()
{
_Valid = false;
}
private static bool Validated(XmlTextReader Xml, XmlTextReader Xsd)
{
var MySchema = XmlSchema.Read(Xsd, new ValidationEventHandler(Invalidated));
var MySettings = new XmlReaderSettings();
{
MySettings.IgnoreComments = true;
MySettings.IgnoreProcessingInstructions = true;
MySettings.IgnoreWhitespace = true;
}
var MyXml = XmlReader.Create(Xml, MySettings);
while (MyXml.Read) {
//Parsing...
}
return _Valid;
}
public static void Main()
{
var XsdPath = "C:\Path\To\MySchemaDocument.xsd";
var XmlPath = "C:\Path\To\MyXmlDocument.xml";
var XsdDoc = new XmlTextReader(XsdPath);
var XmlDoc = new XmlTextReader(XmlPath);
var WellFormed = true;
XmlDocument xDoc = new XmlDocument();
try {
xDoc.Load(XmlDoc);
}
catch (XmlException Ex) {
WellFormed = false;
}
if (WellFormed & Validated(XmlDoc, XsdDoc)) {
//Do stuff with my well formed and validated XmlDocument instance...
}
}
}
回答by joedotnot
I would not XDocument.Load(), as per the accepted answer; why would you read the entire file into memory, it could be a huge file?
根据接受的答案,我不会 XDocument.Load();为什么要将整个文件读入内存,它可能是一个巨大的文件?
I'd probably read the first few bytes into a byteArray (it could even be any binary file), convert the byteArray to string
e.g. System.Text.Encoding.ASCII.GetString(byteArray)
,check if the converted string contains the Xml elements you are expecting, only then continue.
我可能会将前几个字节读入 byteArray(它甚至可以是任何二进制文件),将 byteArray 转换为字符串,例如System.Text.Encoding.ASCII.GetString(byteArray)
,检查转换后的字符串是否包含您期望的 Xml 元素,然后继续。