Multimobile Development: Building Applications for any Smartphone
XSD validating problem: default values
Messages   Related Types
This message was discovered on microsoft.public.dotnet.xml.


AL
Hello,
I have a problem in validating xml docs that have been created via a
serialization class or dataset (generated with xsd.exe): when the xsd schema
contains a "default" value for an element or attribute: if the instance XML
doc does not express a value for such an element, validation fails.

The Schema looks like this:

<xs:schema targetNamespace="URN:testdoc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:td="URN:testdoc"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="1.0">
    <xs:element name="TestDoc">
            <xs:complexType>
            <xs:sequence>
                <xs:element name="Paragrafo" type="xs:string"
default="ParText"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Code for serialization:

Dim doc As TestDoc
doc = New TestDoc()
doc.Paragrafo = "ParText"
Dim ser As New Xml.Serialization.XmlSerializer(GetType(TestDoc),
"URN:testdoc")
Dim xw As New XmlTextWriter("c:\temp\testdoc.xml",
System.Text.Encoding.UTF8)
ser.Serialize(xw, doc)

the resulting xml does not express the "Paragrafo" element, if code fills in
a value equal to the default (seee above example), or if it does not fill a
value for that element:

<?xml version="1.0" encoding="utf-8"?>
<TestDoc xmlns:xsd="http://www.w3.org/2001/XMLSchema";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns="URN:testdoc"/>

So, when I try to validate it, I get errors (missing element).Of course, if
I put a different value, all works pretty good.
So the question is: how can I validate such a doc? I believe I should use
XMLValidatingReader, but miss something: should I "repair", that is fill
missing values, the XML doc before validating it? And what's the correct way
to do this?

Any hint, sample code and doc reference will be greatly appreciated.

thank you,
AL.
Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
SQL Server Development Team [MSFT]
XmlDocument handle default attributes, but not default elements. A
workaround you could use is to catch the validation errors through register
your own ValidationEventHandler and skip such errors.
Hope this helps

Lanqing Dai

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"AL" <Click here to reveal e-mail address> wrote in message
news:Xns9337BB18AA010av926BLOCKSPAMhotmai@207.46.248.16...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
 
System.Text.Encoding
System.Web.UI.WebControls.Xml
System.Xml.Schema.ValidationEventHandler
System.Xml.Serialization.XmlSerializer
System.Xml.XmlDocument
System.Xml.XmlTextWriter




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734