473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Validating Xml with an with the longer form of EndElement fails

' First create Xml document

Dim oXmlDocument As New System.Xml.XmlDocument
oXmlDocument.LoadXml("<Trees><Oak TreeType=""deciduous""></Oak></Trees>")
oXmlDocument.Save("Trees.xml")

' .NET save xml document with spaces

<Trees>
<Oak TreeType=""deciduous"">
</Oak>
</Trees>

' Because there is spaces between <Oak ...and </Oakvalidation against an
xsd schema will now fail. Complains about white space.
' However, if the endelemnt </Oakwas removed and we closed the orginal Oak
like this:

<Trees>
<Oak TreeType=""deciduous""/>
</Trees>

' Validation succeeds. How can this be??

' validation code
Dim oXMLReader As Xml.XmlTextReader
Dim oXMLDoc As Xml.XmlDocument
Dim oXMLValidate As Xml.XmlValidatingReader
oXMLReader = New Xml.XmlTextReader("Trees.xml")
oXMLValidate = New Xml.XmlValidatingReader(oXMLReader)
oXMLValidate.ValidationType = Xml.ValidationType.Schema
dim oSchemaCollection As System.Xml.Schema.XmlSchemaCollection
oSchemaCollection = New System.Xml.Schema.XmlSchemaCollection
oSchemaCollection.Add(Nothing, "Trees.xsd") ' get .Net to autogenerate
oXMLValidate.Schemas.Add(oSchemaCollection)
oXMLDoc = New Xml.XmlDocument
oXMLDoc.Load(oXMLValidate)

Sep 8 '06 #1
4 1288


Labm1ce wrote:

<Trees>
<Oak TreeType=""deciduous""/>
</Trees>
It depends obviously on the schema definition of the Oak element that
you have not shown. If that element has been defined to be empty then it
is not allowed to have any text content, not even white space content.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 8 '06 #2
I think you missed the point of my question.

This doesn't work:
<Oak TreeType=""deciduous"">
</Oak>
This does work:
<Oak TreeType=""deciduous""></Oak>
This also works:
<Oak TreeType=""deciduous""/>

My question is why is the Validating schema giving me a WhiteSpace error and
the Xml that the .Save function created which by the way added the white
space. All three versions of the xml should be acceptable, but I can't
validate the first one.

Here's the xml schema file:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Trees">
<xs:complexType>
<xs:sequence>
<xs:element name="Oak">
<xs:complexType>
<xs:attribute name="TreeType" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

"Martin Honnen" wrote:
It depends obviously on the schema definition of the Oak element that
you have not shown. If that element has been defined to be empty then it
is not allowed to have any text content, not even white space content.
Sep 8 '06 #3


Labm1ce wrote:
I think you missed the point of my question.
No, I don't think so, there is a difference between an element that has
no content at all and one that has white space content when it comes to
schema validation.
My question is why is the Validating schema giving me a WhiteSpace error and
the Xml that the .Save function created which by the way added the white
space. All three versions of the xml should be acceptable, but I can't
validate the first one.
<xs:element name="Oak">
<xs:complexType>
<xs:attribute name="TreeType" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
That way the element is defined to have no content at all, only that
attribute. So the validator is right to complain if there is white space
in the element. The main problem is in my view that the Save method
inserts that white space in the element, that should not happen and is a
bug in my view.

You might be able to avoid the problem of Save inserting white space by
setting
oXmlDocument.PreserveWhitespace = True
before you load that string with LoadXml.

Or you need to change the schema to e.g.

<xs:element name="Oak">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="TreeType" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

then

<Oak TreeType="whatever">
</Oak>

is allowed.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 8 '06 #4
Doing this:
oXmlDocument.PreserveWhitespace = True

as suggested makes the XmlDocument not to do space tabbing. Which is
exactly what I need. :-)

Thanks
Sep 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
0
by: Bradley Bossard via DotNetMonster.com | last post by:
I am having an issue with the .NET framework (or C#) and validating events. I have implemented several validating event handlers for textboxes on a form. When I run the app, the form works...
0
by: Lucilue2003 | last post by:
Hello, I need help validating an xml file against an external DTD. The below code allows me to read the xml file and gather specific elements of interest. If the xml looked like this, How...
5
by: chadwick | last post by:
Hi everyone - I have a form that contains a TabControl object with 2 tab pages, TabPage1 and TabPage2. TabPage1 contains two text boxes and a cancel button, which closes the form. I would like...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.