2013-04-01 24 views
5

Ho un bel file XML in questo modo:c convalida dello schema # 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> 

e poi ho il mio schema che ho creato in questo modo:

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

Per quanto posso vedere guardando che, il file XML è valido perché il primo elemento è Assetd e non patrimoniale, ma se corro il mio codice 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); 

racconta me l'XML è valido ... Se io uso questo codice:

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

ottengo questo output nella 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'. 

Qualcuno può dirmi che cosa sto facendo male per favore? E mi sta uccidendo :(

applausi, /r3plica

+0

"Per quanto posso vedere guardando questo, il il file xml non è valido perché il primo elemento è Assetd e non Asset, ma se eseguo il mio codice C#: "Penso che tu sappia già cosa è sbagliato. – Bit

+0

Non penso che tu capisca. Sto cercando di farlo fallire, eppure afferma che è valido. – r3plica

+0

La seconda parte o nodo è valido. Pensaci. – Bit

risposta

8

è necessario impostare namespace di default nel vostro xml, in questo modo:

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

Inoltre, v'è una serie di altri problemi:

percorso attributo non è definito nello schema, elemento 'Assetd' non è definito maxOccurs = "illimitata" devono essere impostati in schema per xs:. elemento name = "Asset"

012.

Nel caso in cui non è possibile modificare XML, è necessario rimuovere schema di destinazione da xsd:

<xs:schema id="data" 
    xmlns:mstns="http://tempuri.org/data.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 

e registrare schema come questo:

settings.Schemas.Add(null, "data.xsd"); 
+0

grazie, non ho potuto modificare l'XML ma facendo la seconda parte di quello che hai suggerito ha funzionato perfettamente :) – r3plica

Problemi correlati