bonjour,
je voudrais valider un xml via un schema xsd .
J'ai à chaque fois une erreur concernant l'element racine qui est soit-disant non déclaré, si vous pouviez m'éclairer parce que la je tourne en rond et je ne trouve pas :
voila mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 string s ="<?xml version='1.0' encoding='UTF-8'?>"; s +="<query>"; s +="<doctypes>"; s +=" <doctype Id='IDs'/> "; s +="</doctypes>"; s +="<params>"; s +=" <wordparam FieldId='xCollec'>"; s +=" <expression> mon expression </expression>"; s +=" </wordparam>"; s +=" <dateparam FieldId='iDate'>"; s +=" <from>2008-01-01</from>"; s +=" <to>2008-01-31</to>"; s +=" </dateparam>"; s += " <stringparam FieldId='iRef'>"; s +=" <value></value>"; s += " </stringparam>"; s +="</params>"; s += "<orderby FieldId='iDate' Option='descending'/>"; s +="</query>"; //chargement du schema xml XmlSchema xSchema = new XmlSchema(); xSchema.SourceUri = Context.Server.MapPath("~/searchParams.xsd"); XmlReaderSettings xSettings = new XmlReaderSettings(); xSettings.IgnoreComments = true; xSettings.IgnoreWhitespace = true; xSettings.Schemas.Add(xSchema); xSettings.ValidationType = ValidationType.Schema; xSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); XmlReader xReader = XmlReader.Create(new StringReader(s), xSettings); while (xReader.Read()) {} xReader.Close(); private static void ValidationCallback(Object sender, ValidationEventArgs args) { if (sXmlValidationErrors != string.Empty) sXmlValidationErrors += "|"; sXmlValidationErrors += args.Message; }
voici le xsd :
l'erreur de retour est "L'élément 'query' n'est pas déclaré."
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 <?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- ATTRIBUTS --> <xs:attribute name="Id" type="xs:string"/> <xs:attribute name="FieldId" type="xs:string"/> <xs:attribute name="Option" type="xs:string"/> <!-- SIMPLE ELEMENTS --> <xs:element name="expression" type="xs:string"/> <xs:element name="from" type="xs:date"/> <xs:element name="to" type="xs:date"/> <xs:element name="From" type="xs:time"/> <xs:element name="To" type="xs:time"/> <xs:element name="value" type="xs:string"/> <!-- EMPTY ELEMENTS LVL 2--> <xs:element name="doctype"> <xs:complexType> <xs:attribute ref="Id" use="required"/> </xs:complexType> </xs:element> <!-- EMPTY ELEMENTS LVL 1--> <xs:element name="orderby"> <xs:complexType> <xs:attribute ref="FieldId" use="required"/> <xs:attribute ref="Option" use="required"/> </xs:complexType> </xs:element> <!-- COMPLEX ELEMENTS LVL 2 --> <xs:element name="wordparam"> <xs:complexType> <xs:sequence> <xs:element ref="expression"/> </xs:sequence> <xs:attribute ref="FieldId" use="required"/> </xs:complexType> </xs:element> <xs:element name="csvparam"> <xs:complexType> <xs:sequence> <xs:element ref="value"/> </xs:sequence> <xs:attribute ref="FieldId" use="required"/> </xs:complexType> </xs:element> <xs:element name="stringparam"> <xs:complexType> <xs:sequence> <xs:element ref="value"/> </xs:sequence> <xs:attribute ref="FieldId" use="required"/> </xs:complexType> </xs:element> <xs:element name="dateparam"> <xs:complexType> <xs:sequence> <xs:element ref="from"/> <xs:element ref="to"/> </xs:sequence> <xs:attribute ref="FieldId" use="required"/> </xs:complexType> </xs:element> <xs:element name="timeparam"> <xs:complexType> <xs:sequence> <xs:element ref="From"/> <xs:element ref="To"/> </xs:sequence> <xs:attribute ref="FieldId" use="required"/> </xs:complexType> </xs:element> <!-- COMPLEX ELEMENTS LVL 1 --> <xs:element name="doctypes"> <xs:complexType> <xs:sequence> <xs:element ref="doctype"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="params"> <xs:complexType> <xs:sequence> <xs:element ref="wordparam" minOccurs="0"/> <xs:element ref="dateparam" minOccurs="0"/> <xs:element ref="timeparam" minOccurs="0"/> <xs:element ref="csvparam" minOccurs="0"/> <xs:element ref="stringparam" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> <!-- PRINCIPAL --> <xs:element name="query"> <xs:complexType> <xs:sequence> <xs:element ref="doctypes"/> <xs:element ref="params"/> <xs:element ref="orderby"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
ce qui est le plus etrange c que quand je fais le test directement dans un logiciel de traitement xml ( stylus ) , la validation se passe sans probleme !!!!
par avance merci pour votre aide
A++
Partager