473,660 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validates in XML Spy but generates errors in .Net

Hi. I have a simple schema which validates fine in XML Spy but in .Net
I get a Type Exception:
[The 'startDate' element has an invalid value according to its data
type]

The effect I'm looking for is to be able to pass an empty date.

The Schema File:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied">
<xs:element name="search">
<xs:complexType >
<xs:sequence>
<xs:element name="agentNo" type="xs:string "/>
<xs:element name="attention " type="xs:string "/>
<xs:element name="agentName " type="xs:string "/>
<xs:element name="orgNo" type="xs:string "/>
<xs:element name="startDate " type="euroSearc hDate"/>
<xs:element name="stopDate" type="euroSearc hDate"/>
<xs:element name="country"/>
<xs:element name="companyCo de"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleTyp e name="euroSearc hDate">
<xs:union memberTypes="eu roEmptyString euroDate"/>
</xs:simpleType>

<xs:simpleTyp e name="euroDate" >
<xs:restricti on base="xs:date"/>
</xs:simpleType>

<xs:simpleTyp e name="euroEmpty String">
<xs:restricti on base="xs:string ">
<xs:enumerati on value=""/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

The xml File:
<?xml version="1.0" encoding="UTF-8"?>
<search xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<agentNo></agentNo>
<attention></attention>
<agentName></agentName>
<orgNo></orgNo>
<startDate></startDate>
<stopDate></stopDate>
<country></country>
<companyCode> </companyCode>
</search>

The C# code:

XmlTextReader xmlReader = new XmlTextReader(@ "myxmlFile.xml" );
XmlDocument xmlSoapDoc = new XmlDocument();
xmlSoapDoc.Load (xmlReader);
//validate against a schema
try
{
Validate(xmlSoa pDoc,@"mySchema .xsd");
}
catch (System.Excepti on valException)
{
throw valException;
}
return xmlSoapDoc.Oute rXml;
}
public void Validate(XmlDoc ument xmlDoc, String xsdPath)
{
//move the xml to a stream
System.IO.Memor yStream streamXML = new System.IO.Memor yStream();
xmlDoc.Save(str eamXML);
streamXML.Posit ion = 0;

//create an xmlTextReader from the stream
XmlTextReader readerXML = new XmlTextReader(s treamXML);

//create a new schema collection
XmlSchemaCollec tion xsds = new XmlSchemaCollec tion();

//add our schema
try
{
xsds.Add(null, xsdPath);
}
catch(System.Ex ception xsdError)
{
throw xsdError;
}

//create a new xmlValidatingRe ader
XmlValidatingRe ader xmlValidator = new XmlValidatingRe ader(readerXML) ;
xmlValidator.Va lidationType = ValidationType. Schema;

//add our schema collection to the validator
xmlValidator.Sc hemas.Add(xsds) ;
//add validation Error handler
xmlValidator.Va lidationEventHa ndler += new
ValidationEvent Handler(Validat ionCallBack);
try
{
while (xmlValidator.R ead());
}
catch (System.Excepti on valException)
{
throw valException;
}
}

public static void ValidationCallB ack(object sender,
ValidationEvent Args e)
{
System.Exceptio n valException = new Exception(e.Mes sage);
throw valException;
}

}
}

Thanks - I'm lost!
/Haddock

Nov 12 '05 #1
1 1418
It is because you are loading the XML in a XmlDocument, which has
PreserveWhitesp aces set to false by default, so as a results
<startDate></startDate>

becomes

<startDate>
</startDate>

notice there is a newline and carriage return characeter added to the
element content. This will fail validation since this type does not accept
that value. You need to set PreserveWhitesp ace to true on the Document
before loading it like:
xmlSoapDoc.Pres erveWhitespace = true;

Thanks.

"Kapten Haddock" <an****@tropica lstorm.se> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hi. I have a simple schema which validates fine in XML Spy but in .Net
I get a Type Exception:
[The 'startDate' element has an invalid value according to its data
type]

The effect I'm looking for is to be able to pass an empty date.

The Schema File:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied">
<xs:element name="search">
<xs:complexType >
<xs:sequence>
<xs:element name="agentNo" type="xs:string "/>
<xs:element name="attention " type="xs:string "/>
<xs:element name="agentName " type="xs:string "/>
<xs:element name="orgNo" type="xs:string "/>
<xs:element name="startDate " type="euroSearc hDate"/>
<xs:element name="stopDate" type="euroSearc hDate"/>
<xs:element name="country"/>
<xs:element name="companyCo de"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleTyp e name="euroSearc hDate">
<xs:union memberTypes="eu roEmptyString euroDate"/>
</xs:simpleType>

<xs:simpleTyp e name="euroDate" >
<xs:restricti on base="xs:date"/>
</xs:simpleType>

<xs:simpleTyp e name="euroEmpty String">
<xs:restricti on base="xs:string ">
<xs:enumerati on value=""/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

The xml File:
<?xml version="1.0" encoding="UTF-8"?>
<search xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<agentNo></agentNo>
<attention></attention>
<agentName></agentName>
<orgNo></orgNo>
<startDate></startDate>
<stopDate></stopDate>
<country></country>
<companyCode> </companyCode>
</search>

The C# code:

XmlTextReader xmlReader = new XmlTextReader(@ "myxmlFile.xml" );
XmlDocument xmlSoapDoc = new XmlDocument();
xmlSoapDoc.Load (xmlReader);
//validate against a schema
try
{
Validate(xmlSoa pDoc,@"mySchema .xsd");
}
catch (System.Excepti on valException)
{
throw valException;
}
return xmlSoapDoc.Oute rXml;
}
public void Validate(XmlDoc ument xmlDoc, String xsdPath)
{
//move the xml to a stream
System.IO.Memor yStream streamXML = new System.IO.Memor yStream();
xmlDoc.Save(str eamXML);
streamXML.Posit ion = 0;

//create an xmlTextReader from the stream
XmlTextReader readerXML = new XmlTextReader(s treamXML);

//create a new schema collection
XmlSchemaCollec tion xsds = new XmlSchemaCollec tion();

//add our schema
try
{
xsds.Add(null, xsdPath);
}
catch(System.Ex ception xsdError)
{
throw xsdError;
}

//create a new xmlValidatingRe ader
XmlValidatingRe ader xmlValidator = new XmlValidatingRe ader(readerXML) ;
xmlValidator.Va lidationType = ValidationType. Schema;

//add our schema collection to the validator
xmlValidator.Sc hemas.Add(xsds) ;
//add validation Error handler
xmlValidator.Va lidationEventHa ndler += new
ValidationEvent Handler(Validat ionCallBack);
try
{
while (xmlValidator.R ead());
}
catch (System.Excepti on valException)
{
throw valException;
}
}

public static void ValidationCallB ack(object sender,
ValidationEvent Args e)
{
System.Exceptio n valException = new Exception(e.Mes sage);
throw valException;
}

}
}

Thanks - I'm lost!
/Haddock

Nov 12 '05 #2

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

Similar topics

0
1375
by: Martin | last post by:
asp.net 2.0 I have implemented the url rewriting using httpcontext.rewrite path( newpath, setbase) which all works fine in IE, but testing with fiddler ( www.fiddlertool.com) when you test Mozilla 5.0 UserAgent - it generates a http 500 error. This is bad because none of those pages will get indexed by google or Yahoo etc and any other browser type which is effected by this bug. Do we have a patch or fix for this ? Thanks
1
2464
by: Martin Z | last post by:
I'm getting acquainted with the whole XML/XSD thing, and I've run into a wall. I have a tree of objects that I deserialize from XML for configuration reasons. I have generated XSD from the classes for this tree. I have made an XML file that validates against that XSD. However, when I try to deserialize from the XML file, I get complaints that System.InvalidOperationException was unhandled Message="There is an error in XML document...
6
2480
by: yaru22 | last post by:
I'd like to create a program that validates bunch of urls against the w3c markup validator (http://validator.w3.org/) and store the result in a file. Since I don't know network programming, I have no idea how to start coding this program. I was looking at the python library and thought urllib or urllib2 may be used to make this program work.
2
2378
by: Jake Barnes | last post by:
Please check out this page: http://www.bluewallllc.com/Laura/cms/index.php?pageId=2217 It validates, and it is rendering about right in FireFox. However, in IE it has a bunch of extra padding that is throwing off the whole page. In particular, it seems there is more padding on the left of the main container. Even when we go through and set most of the padding to 0, so that in FireFox there is almost no padding, in IE there is still...
3
9922
by: Eric Lilja | last post by:
Sorry for asking so many questions, but I've just started and need to get some things working so I can do the task that is before me. Consider this (validating) schema: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="myns" xmlns="myns" elementFormDefault="qualified"> <xs:element name="books"> <xs:complexType> <xs:sequence>
8
3917
by: Radu | last post by:
Hi. I have an ASP control on my page: <asp:Calendar ID="calStart" ................ Etc </asp:Calendar> and I have a Custom Validator defined as <asp:CustomValidator
0
978
by: draconas | last post by:
Hi all As part of a project I have a vb program that takes an XML document (payload-XML), which contains a lot of data, but no namespaces. It is run through an XSLT transformation which creates an XML message (Message XML) to be dilivered to another system, then that XML message is validated in code. The Message XML to the other system does use namespaces and XSD validation, and one of its XML fields is the original XML document, in it's...
1
1418
by: postmanpat | last post by:
i have to create a login form that validates the users and passwords from a text file. I have another function that can add new users and passwords by writing to a test file split by a delimiter. But i dont know how to read from the text file and validates one user one by one when i key in in the login form. the example format of the text file: peter|123 jane|789 hunter|007...left is the username and password is on the right. i dont need sql...
2
2100
by: Bruno Schneider | last post by:
I've seen this page, that seems invalid to me. Doctype is incomplete and it does not have a <bodytag. However, W3C validator validates it as HTML 4.01 strict, even when I force the DOCTYPE. I think it should be a bug in the validator, but perhaps, I missed something. What do you think? Page: http://www.bcc.ufla.br/~lpgomes/ Validator:
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.