EDN Admin
Well-known member
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small Actually, this is not a purly question. I just want to have a disucssion here and get some deeper insight to the issue.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small First, let me brief the issue. There is a XML schema Schema.xsd, and an XML file File1.xml. One point is that in the schema file, no namespace is used. Thatâs to say, the
targetNamespace is not used. In my understanding, the âabsence namespaceâ is actually used in this way. The schema is like this:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <?<span style="font-family:新宋体; color:#a31515; font-size:10pt xml<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt version<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue 1.0"<span style="color:blue
<span style="color:red encoding<span style="color:blue ="<span style="color:blue UTF-8"<span style="color:blue ?>
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:schema<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt xmlns:xs<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema "<span style="color:blue
<span style="color:red elementFormDefault<span style="color:blue ="<span style="color:blue qualified"<span style="color:blue
<span style="color:red attributeFormDefault<span style="color:blue ="<span style="color:blue unqualified"<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:element<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt name<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue ImportInfo"<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:complexType<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:sequence<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:element<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt ref<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue SchemaVersion"<span style="color:blue />
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:element<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt ref<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue EquipmentList"<span style="color:blue />
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt </<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:sequence<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="text-indent:21pt; margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt </<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:complexType<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="text-indent:21pt; margin:0in 0in 0pt <span style="font-family:"Times New Roman","serif"; color:blue; font-size:10pt â<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="text-indent:21pt; margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small Then, the File1.xml doesnât contain anything related to this schema. In other words, it is completely an irrelevant xml file which are picked by our test engineer in random.
So, we hope the following c# code can help to validate the file is illegal:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt 0.5in <span style="font-family:新宋体; color:#2b91af; font-size:10pt XmlReaderSettings<span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings = <span style="color:blue new
<span style="color:#2b91af XmlReaderSettings();
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings.<span style="color:#010001 Schemas.<span style="color:#010001 Add(<span style="color:blue null,
<span style="color:#010001 pSchemaFile);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings.<span style="color:#010001 ValidationType =
<span style="color:#2b91af ValidationType.<span style="color:#010001 Schema;
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:green // Set the validation event handler
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings.<span style="color:#010001 ValidationEventHandler +=
<span style="color:blue new <span style="color:#2b91af ValidationEventHandler(<span style="color:#010001 SettingsValidationEventHandler);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:green // Read XML data
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#2b91af XmlReader <span style="color:#010001 reader =
<span style="color:#2b91af XmlReader.<span style="color:#010001 Create(<span style="color:#010001 pFileName,
<span style="color:#010001 settings);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue try
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue while (<span style="color:#010001 reader.<span style="color:#010001 Read())
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue catch (<span style="color:#2b91af XmlException
<span style="color:#010001 ex)
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue throw <span style="color:blue new <span style="color:#2b91af
FTLImportExportException(<span style="color:#a31515 message, caption);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small The event handler is:
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="text-indent:0.5in; margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt private<span style="font-family:新宋体; font-size:10pt
<span style="color:blue void <span style="color:#010001 SettingsValidationEventHandler(<span style="color:blue object
<span style="color:#010001 sender, <span style="color:#2b91af ValidationEventArgs
<span style="color:#010001 e)
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue string <span style="color:#010001 msg =
<span style="font-family:"Times New Roman","serif"; color:#a31515; font-size:10pt â<span style="font-family:新宋体; color:#a31515; font-size:10pt ;<span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue string <span style="color:#010001 caption =
<span style="font-family:"Times New Roman","serif"; color:#010001; font-size:10pt â<span style="font-family:新宋体; font-size:10pt ;
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue if (<span style="color:#010001 e.<span style="color:#010001 Severity ==
<span style="color:#2b91af XmlSeverityType.<span style="color:#010001 Error)
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue new <span style="color:#2b91af FTLImportExportException(<span style="color:#010001 msg +
<span style="color:#010001 e.<span style="color:#010001 Message, <span style="color:#010001
caption);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small However, the File1.xml passed this validation and no exception is thrown. Letâs have a simple look at the xml file:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:LGCatalogSnippet<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt xsi:schemaLocation<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue urn:horizon:loggraphics LgSchema3.xsd"<span style="color:blue
<span style="color:red xmlns:lg<span style="color:blue ="<span style="color:blue urn:horizon:loggraphics"<span style="color:blue
<span style="color:red xmlns:xsi<span style="color:blue ="<span style="color:blue http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance "<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:Formats<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:Format<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt lg:UniqueId<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue PLxPRESS_SPRINT_LQC"<span style="color:blue
<span style="color:red xmlns:lg<span style="color:blue ="<span style="color:blue urn:horizon:loggraphics"<span style="color:blue
<span style="color:red xmlns:xsi<span style="color:blue ="<span style="color:blue http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance "<span style="color:blue
<span style="color:red xsi:schemaLocation<span style="color:blue ="<span style="color:blue urn:horizon:loggraphics LgSchema3.xsd"<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lgescription<span style="font-family:新宋体; color:blue; font-size:10pt ><span style="font-family:新宋体; font-size:10pt DEPTH<span style="color:blue </<span style="color:#a31515 lgescription<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:IndexUnits<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:IndexUnit<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt lg:UnitSystem<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue english"<span style="color:blue >ft<span style="color:blue </<span style="color:#a31515 lg:IndexUnit<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:"Times New Roman","serif"; color:blue; font-size:10pt â<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt </<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:IndexUnits<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:"Times New Roman","serif"; color:blue; font-size:10pt â<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small Anyway, the content in this xml is totally irrelevant to the schema. Particularly, we hope there is mandatory one
ImportInfo according to the schema. How can the validation succeed? Also, if we try to perform the validation in XML spy, it will fail first when detecting
lg:LGCatalogSnippet is not defined in the schema at all. So, there must be something tricky in the validation provided by XmlReader.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small With some valuable information provided in the former discussion session by He Hongjie and Bai Jie, after some investigation and experiments, I think two points are important in
the XmlReaderâs validation:
<p style="text-indent:-0.25in; margin:0in 0in 0pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-family:Calibri; font-size:small In the above code, the validation only cares about the things which have the same namespace as the schema. So, the schema uses âabsence namespaceâ, and all of the elements in
the File1.xml are in lg namespace. Then, the xml file is just like an empty file for the validation. So, it can pass.
<p style="text-indent:-0.25in; margin:0in 0in 0pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-family:Calibri; font-size:small According to MSDN, XmlReader is a âfast, non-cached, forward-only access to XML dataâ. Thatâs to say, it has not the ability to turn back its head to see whether âthere
is an ImportInfoâ after finishing the scanning of the whole xml. The while-loop also implied this.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small The fix of this issue is actually quite simple. (Unfortunately, there is not a good documentation on this tricky difference). Add the following code to XmlReaderSettings:
<p style="margin:0in 0in 0pt <span style="font-family:Consolas; color:black; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Consolas; color:black; font-size:10pt settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small And add the following code to the event handler:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt 0.5in <span style="font-family:新宋体; color:blue; font-size:10pt Else
<span style="font-family:新宋体; color:#00b050; font-size:10pt // e.Severity == XmlSeverityType.Warning<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 caption = <span style="color:#a31515 "Schema Validation Warning";
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small After this, in the validation process, when encountering the first element, which is
<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:LGCatalogSnippet<span style="font-family:Calibri; font-size:small , a warning âWarning: Could not find schema information for the element â.â will be reported
because it is not defined in the schema. Unfortunately, there is not good documentation to say what the
<span style="font-family:Consolas; color:black; font-size:10pt ReportValidationWarnings
<span style="font-family:Calibri; font-size:small means in MSDN. We donât know what kind of validation issues are treated as Error, and what others are treated as Warning. At least I didnât find such a documentation in my dayâs
investigation.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small A good discussion session related to this topic in MSDN Forum is:
http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/1b463a9c-d0d8-4a5d-b1cd-98f5a0663b59 <span style="font-family:Calibri; color:#800080; font-size:small http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/1b463a9c-d0d8-4a5d-b1cd-98f5a0663b59 <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-size:small <span style="font-family:Calibri My expertise on XML Schema is quite limited, and I only focused on this special issue to perform the investigation (maybe, I can only see one tree, instead of the
whole forest, <span style="font-family:Wingdings J<span style="font-family:Calibri ). So, the above analysis might have some flaws. If somebody can give some complementary comment and help us get a more thorough and deep understanding, I will
appreciate it very much.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
View the full article
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small First, let me brief the issue. There is a XML schema Schema.xsd, and an XML file File1.xml. One point is that in the schema file, no namespace is used. Thatâs to say, the
targetNamespace is not used. In my understanding, the âabsence namespaceâ is actually used in this way. The schema is like this:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <?<span style="font-family:新宋体; color:#a31515; font-size:10pt xml<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt version<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue 1.0"<span style="color:blue
<span style="color:red encoding<span style="color:blue ="<span style="color:blue UTF-8"<span style="color:blue ?>
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:schema<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt xmlns:xs<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema "<span style="color:blue
<span style="color:red elementFormDefault<span style="color:blue ="<span style="color:blue qualified"<span style="color:blue
<span style="color:red attributeFormDefault<span style="color:blue ="<span style="color:blue unqualified"<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:element<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt name<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue ImportInfo"<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:complexType<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:sequence<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:element<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt ref<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue SchemaVersion"<span style="color:blue />
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:element<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt ref<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue EquipmentList"<span style="color:blue />
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt </<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:sequence<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="text-indent:21pt; margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt </<span style="font-family:新宋体; color:#a31515; font-size:10pt xs:complexType<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="text-indent:21pt; margin:0in 0in 0pt <span style="font-family:"Times New Roman","serif"; color:blue; font-size:10pt â<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="text-indent:21pt; margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small Then, the File1.xml doesnât contain anything related to this schema. In other words, it is completely an irrelevant xml file which are picked by our test engineer in random.
So, we hope the following c# code can help to validate the file is illegal:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt 0.5in <span style="font-family:新宋体; color:#2b91af; font-size:10pt XmlReaderSettings<span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings = <span style="color:blue new
<span style="color:#2b91af XmlReaderSettings();
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings.<span style="color:#010001 Schemas.<span style="color:#010001 Add(<span style="color:blue null,
<span style="color:#010001 pSchemaFile);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings.<span style="color:#010001 ValidationType =
<span style="color:#2b91af ValidationType.<span style="color:#010001 Schema;
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:green // Set the validation event handler
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 settings.<span style="color:#010001 ValidationEventHandler +=
<span style="color:blue new <span style="color:#2b91af ValidationEventHandler(<span style="color:#010001 SettingsValidationEventHandler);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:green // Read XML data
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#2b91af XmlReader <span style="color:#010001 reader =
<span style="color:#2b91af XmlReader.<span style="color:#010001 Create(<span style="color:#010001 pFileName,
<span style="color:#010001 settings);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue try
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue while (<span style="color:#010001 reader.<span style="color:#010001 Read())
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue catch (<span style="color:#2b91af XmlException
<span style="color:#010001 ex)
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue throw <span style="color:blue new <span style="color:#2b91af
FTLImportExportException(<span style="color:#a31515 message, caption);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small The event handler is:
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="text-indent:0.5in; margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt private<span style="font-family:新宋体; font-size:10pt
<span style="color:blue void <span style="color:#010001 SettingsValidationEventHandler(<span style="color:blue object
<span style="color:#010001 sender, <span style="color:#2b91af ValidationEventArgs
<span style="color:#010001 e)
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue string <span style="color:#010001 msg =
<span style="font-family:"Times New Roman","serif"; color:#a31515; font-size:10pt â<span style="font-family:新宋体; color:#a31515; font-size:10pt ;<span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue string <span style="color:#010001 caption =
<span style="font-family:"Times New Roman","serif"; color:#010001; font-size:10pt â<span style="font-family:新宋体; font-size:10pt ;
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue if (<span style="color:#010001 e.<span style="color:#010001 Severity ==
<span style="color:#2b91af XmlSeverityType.<span style="color:#010001 Error)
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:blue new <span style="color:#2b91af FTLImportExportException(<span style="color:#010001 msg +
<span style="color:#010001 e.<span style="color:#010001 Message, <span style="color:#010001
caption);
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small However, the File1.xml passed this validation and no exception is thrown. Letâs have a simple look at the xml file:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:LGCatalogSnippet<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt xsi:schemaLocation<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue urn:horizon:loggraphics LgSchema3.xsd"<span style="color:blue
<span style="color:red xmlns:lg<span style="color:blue ="<span style="color:blue urn:horizon:loggraphics"<span style="color:blue
<span style="color:red xmlns:xsi<span style="color:blue ="<span style="color:blue http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance "<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:Formats<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:Format<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt lg:UniqueId<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue PLxPRESS_SPRINT_LQC"<span style="color:blue
<span style="color:red xmlns:lg<span style="color:blue ="<span style="color:blue urn:horizon:loggraphics"<span style="color:blue
<span style="color:red xmlns:xsi<span style="color:blue ="<span style="color:blue http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance "<span style="color:blue
<span style="color:red xsi:schemaLocation<span style="color:blue ="<span style="color:blue urn:horizon:loggraphics LgSchema3.xsd"<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lgescription<span style="font-family:新宋体; color:blue; font-size:10pt ><span style="font-family:新宋体; font-size:10pt DEPTH<span style="color:blue </<span style="color:#a31515 lgescription<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:IndexUnits<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt <<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:IndexUnit<span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:新宋体; color:red; font-size:10pt lg:UnitSystem<span style="font-family:新宋体; color:blue; font-size:10pt =<span style="font-family:新宋体; font-size:10pt "<span style="color:blue english"<span style="color:blue >ft<span style="color:blue </<span style="color:#a31515 lg:IndexUnit<span style="color:blue >
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt
<span style="font-family:"Times New Roman","serif"; color:blue; font-size:10pt â<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt </<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:IndexUnits<span style="font-family:新宋体; color:blue; font-size:10pt >
<p style="margin:0in 0in 0pt <span style="font-family:"Times New Roman","serif"; color:blue; font-size:10pt â<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small Anyway, the content in this xml is totally irrelevant to the schema. Particularly, we hope there is mandatory one
ImportInfo according to the schema. How can the validation succeed? Also, if we try to perform the validation in XML spy, it will fail first when detecting
lg:LGCatalogSnippet is not defined in the schema at all. So, there must be something tricky in the validation provided by XmlReader.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small With some valuable information provided in the former discussion session by He Hongjie and Bai Jie, after some investigation and experiments, I think two points are important in
the XmlReaderâs validation:
<p style="text-indent:-0.25in; margin:0in 0in 0pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-family:Calibri; font-size:small In the above code, the validation only cares about the things which have the same namespace as the schema. So, the schema uses âabsence namespaceâ, and all of the elements in
the File1.xml are in lg namespace. Then, the xml file is just like an empty file for the validation. So, it can pass.
<p style="text-indent:-0.25in; margin:0in 0in 0pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-family:Calibri; font-size:small According to MSDN, XmlReader is a âfast, non-cached, forward-only access to XML dataâ. Thatâs to say, it has not the ability to turn back its head to see whether âthere
is an ImportInfoâ after finishing the scanning of the whole xml. The while-loop also implied this.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small The fix of this issue is actually quite simple. (Unfortunately, there is not a good documentation on this tricky difference). Add the following code to XmlReaderSettings:
<p style="margin:0in 0in 0pt <span style="font-family:Consolas; color:black; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Consolas; color:black; font-size:10pt settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small And add the following code to the event handler:
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt 0.5in <span style="font-family:新宋体; color:blue; font-size:10pt Else
<span style="font-family:新宋体; color:#00b050; font-size:10pt // e.Severity == XmlSeverityType.Warning<span style="font-family:新宋体; color:blue; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt {
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<span style="color:#010001 caption = <span style="color:#a31515 "Schema Validation Warning";
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt }
<p style="margin:0in 0in 0pt <span style="font-family:新宋体; font-size:10pt
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small After this, in the validation process, when encountering the first element, which is
<span style="font-family:新宋体; color:#a31515; font-size:10pt lg:LGCatalogSnippet<span style="font-family:Calibri; font-size:small , a warning âWarning: Could not find schema information for the element â.â will be reported
because it is not defined in the schema. Unfortunately, there is not good documentation to say what the
<span style="font-family:Consolas; color:black; font-size:10pt ReportValidationWarnings
<span style="font-family:Calibri; font-size:small means in MSDN. We donât know what kind of validation issues are treated as Error, and what others are treated as Warning. At least I didnât find such a documentation in my dayâs
investigation.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small A good discussion session related to this topic in MSDN Forum is:
http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/1b463a9c-d0d8-4a5d-b1cd-98f5a0663b59 <span style="font-family:Calibri; color:#800080; font-size:small http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/1b463a9c-d0d8-4a5d-b1cd-98f5a0663b59 <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-size:small <span style="font-family:Calibri My expertise on XML Schema is quite limited, and I only focused on this special issue to perform the investigation (maybe, I can only see one tree, instead of the
whole forest, <span style="font-family:Wingdings J<span style="font-family:Calibri ). So, the above analysis might have some flaws. If somebody can give some complementary comment and help us get a more thorough and deep understanding, I will
appreciate it very much.
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 0pt <span style="font-family:Calibri; font-size:small
View the full article