C#XML Schema验证 [英] c# XML Schema validation
问题描述
我有这样一个很好的XML文件:
I have a nice XML file like this:
<?xml version="1.0" encoding="utf-8" ?>
<Assets Path="C:\Users\r3plica\Google Drive">
<Assetd>
<FileName>Boomerang - Error codes.xlsx</FileName>
<DisplayName>Boomerang - Error codes</DisplayName>
<Description>This is the Boomerang error codes file</Description>
<Tags>
<Tag>Excel</Tag>
<Tag>Boomerang</Tag>
</Tags>
<Categories>
<Category>1</Category>
<Category>4</Category>
</Categories>
</Assetd>
<Asset>
<FileName>Issue Tracker v5.xlsx</FileName>
<Description>This is the issue tracker for Skipstone</Description>
<Tags>
<Tag>Excel</Tag>
<Tag>Skipstone</Tag>
</Tags>
<Categories>
<Category>1</Category>
<Category>4</Category>
</Categories>
</Asset>
</Assets>
然后我有我的方案,我喜欢这样创建:
and then I have my schema which I created like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="data"
targetNamespace="http://tempuri.org/data.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/data.xsd"
xmlns:mstns="http://tempuri.org/data.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Assets">
<xs:complexType>
<xs:sequence>
<xs:element name="Asset" type="Asset" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Asset">
<xs:sequence>
<xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="DisplayName" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="Tags" type="Tags" minOccurs="0" maxOccurs="1" />
<xs:element name="Categories" type="Categories" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Tags">
<xs:sequence>
<xs:element name="Tag" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Categories">
<xs:sequence>
<xs:element name="Category" type="xs:int" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
据我可以看到在看的是,XML文件是无效的,因为第一个元素是Assetd而不是资产,但如果我跑我的C#代码:
As far as I can see looking at that, the xml file is invalid because the first element is Assetd and not Asset, but if I run my c# code:
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://tempuri.org/data.xsd", "data.xsd");
XDocument doc = XDocument.Load(openFileDialog1.FileName);
string msg = "";
doc.Validate(schemas, (o, err) =>
{
msg = err.Message;
});
Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg);
它告诉我,XML是有效的...
。如果我用这个代码:
it tells me the xml is valid... If I use this code:
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("http://tempuri.org/data.xsd", "data.xsd");
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create(openFileDialog1.FileName, settings);
// Parse the file.
while (reader.Read()) ;
我得到在控制台输出如下:
I get this output in the console:
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Assets'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the attribute 'Path'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Assetd'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'FileName'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'DisplayName'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Description'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tags'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Categories'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Asset'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'FileName'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Description'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tags'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Categories'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'.
谁能告诉我什么,我做错了吗?这是我的命:(
Can anyone tell me what I am doing wrong please? It is killing me :(
欢呼声中,
/ r3plica
cheers, /r3plica
推荐答案
您需要设置默认的命名空间在XML中,是这样的:
You need to set default namespace in your xml, like this:
<?xml version="1.0" encoding="utf-8" ?>
<Assets xmlns="http://tempuri.org/data.xsd">
<Asset>
<FileName>Boomerang - Error codes.xlsx</FileName>
<DisplayName>Boomerang - Error codes</DisplayName>
<Description>This is the Boomerang error codes file</Description>
<Tags>
<Tag>Excel</Tag>
<Tag>Boomerang</Tag>
</Tags>
<Categories>
<Category>1</Category>
<Category>4</Category>
</Categories>
</Asset>
<Asset>
<FileName>Issue Tracker v5.xlsx</FileName>
<Description>This is the issue tracker for Skipstone</Description>
<Tags>
<Tag>Excel</Tag>
<Tag>Skipstone</Tag>
</Tags>
<Categories>
<Category>1</Category>
<Category>4</Category>
</Categories>
</Asset>
</Assets>
此外,还有一些其他的问题:
Also, there is a number of other problems:
Path属性没有在模式中定义,Assetd'元素没有定义。
的maxOccurs =无界需要在架构中设置为XS:元素的名称=资产
Path attribute is not defined in schema, 'Assetd' element is not defined. maxOccurs="unbounded" need to be set in schema for xs:element name="Asset"
在的情况下,如果你不能修改XML,你需要从XSD删除目标模式:
In case if you cannot modify xml, you need to remove target schema from xsd:
<xs:schema id="data"
xmlns:mstns="http://tempuri.org/data.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
和这样的注册模式:
settings.Schemas.Add(null, "data.xsd");
这篇关于C#XML Schema验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!