2013-04-01 24 views
5

は、私はこのような素敵なXMLファイルを持っている:C#のXMLスキーマ検証

<?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> 

をして、私は次のように作成した自分のスキーマを持っている:

<?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> 

私の知る限り見て見ることができるように最初の要素がAssetdない資産であるため、それは、xmlファイルは無効ですが、私は私のC#のコードを実行する場合:

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); 

はそれが告げます私は、コンソールでこの出力を得る

// 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()) ; 

:私はこのコードを使用している場合、私は、XMLは... 有効です

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'. 

誰もが私が間違って下さいやっているものを私に言うことはできますか?それは、私を殺している:(

歓声を、 /r3plica

+0

「私の知る限りそれを見て見ることができるように、最初の要素はAssetdでAssetであるため、xmlファイルは無効ですが、C#コードを実行すると、「あなたは何が間違っているのかはすでに分かっていると思います。 – Bit

+0

私はあなたが理解しているとは思わない。私はそれが失敗するようにしようとしていますが、それは有効であると述べています。 – r3plica

+0

2番目の部分またはノードが有効です。それについて考える。 – Bit

答えて

8

あなたはこのように、あなたのXMLに既定の名前空間を設定する必要があります。また

<?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> 

を、他の多くの問題があります:

Path属性がスキーマで定義されていない、「Assetd」要素が定義されていません のmaxOccurs =「無制限」XSのスキーマに設定する必要があります。要素名=「資産」

場合

あなたはXMLを変更できない場合、あなたは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"); 
+0

ありがとう、私はXMLを変更することができませんでしたが、あなたが完全に働くことを示唆したの第二の部分をやっている:) – r3plica

関連する問題