xml validation  
Author Message
suryany





PostPosted: XML and the .NET Framework, xml validation Top

hi,

i have to validate xml document in different scenarios.

1) when xml document changes into message

2) when message changes into xml document

3) when xml document loads into database

4) when xml document comes from database

i learnt there are two options

1)validating it at the time of loading into the object

2) after loading of xml into object

in this link http://www.hide-link.com/

using xmltextreader to validate xml document is better than using xmlvalidation reader mentioned.

how it is possible



.NET Development2  
 
 
Sergey Dubinets - MSFT





PostPosted: XML and the .NET Framework, xml validation Top

In .NET Framework you can validate XML by reading it with XmlReader. To create "validating reader" use XmlReader.Create() with appropriate arguments.

In your scenarios you would need create somehow XmlReader that represents your data and validate this reader with "validating reader": http://msdn2.microsoft.com/en-us/library/56xykaw6.aspx



 
 
Ilya Tumanov





PostPosted: XML and the .NET Framework, xml validation Top

If you mean validation against schema, validating XML reader is available on NETCF V2 and it works the same way as on desktop. There's no way to validate XML on NETCF V1 unless you do it all yourself.



 
 
Sergey Dubinets - MSFT





PostPosted: XML and the .NET Framework, xml validation Top

One link that is very related to your problem:

http://msdn2.microsoft.com/en-us/library/system.xml.schema.xmlschemavalidator.aspx



 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Yeah but to me it doesn't work. I tried this simple example: http://msdn2.microsoft.com/en-us/library/56xykaw6.aspx but it terminates correctly without raising any validation error. I copied the files without any modification but it doesn't work.

Someone has never tried to effective trying to validate an XML against a schema in CF Any suggestion

Thank you



 
 
Ilya Tumanov





PostPosted: XML and the .NET Framework, xml validation Top

If you copied it without any modifications (really you have to at least correct file paths), the output goes to console. Does your device have console If not, you won't see any output.

 



 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Hi thanks for your help. I only copied the file in the same directory of the exe. I tried to launch this example before local to my computer (by double clicking on the .exe). And it doesn't give any error.

When I try to deploy it on the device it doesn't found the file but they are in the same folder...

Please help :)

Grettings

 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

I created this example as a Windows application and it works. The same example as a Pocket PC 2003 application CF2.0 doesn't validate. So...



 
 
Ilya Tumanov





PostPosted: XML and the .NET Framework, xml validation Top

It could not possibly display any errors as PPC 2003 has no console. Change the code to pop up a message box or set a breakpoint in de****.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

Device has no support for relative paths so putting files to the EXE folder won’t do you any good. Full path has to be specified or files won't be found.

 

One more thing: for some reasons many developers believe device can see desktop’s hard drive by simply specifying “C:\path\file”.

That is of course not the case – device can’t see desktop’s hard drive. So please make sure files are on device file system.



 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Thank you but the problem is that CF don't validate XML against a Schema. Anyway I've installed a prompt on my Ipaq.

Thanks for the tips :)

Hi

 
 
Ilya Tumanov





PostPosted: XML and the .NET Framework, xml validation Top

It does validate. I've just run the code from the sample on device (adjusted for paths and luck of console). Sure enough it shows expected message about attribute. Here’s my code (add two buttons and a textbox to the form, also add XML and XSD files to your project, set action to “Content” and condition to “Deploy always”):

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Xml.Schema;

using System.IO;

using System.Reflection;

namespace ValidateXml

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button2_Click(object sender, EventArgs e)

{

this.Close();

}

private void button1_Click(object sender, EventArgs e)

{

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

// Create and load the XML document.

XmlDocument doc = new XmlDocument();

doc.Load(Path.Combine(path, "booksSchema.xml"));

// Make changes to the document.

XmlElement book = (XmlElement) doc.DocumentElement.FirstChild;

book.SetAttribute("publisher", "Worldwide Publishing");

// Create an XmlNodeReader using the XML document.

XmlNodeReader nodeReader = new XmlNodeReader(doc);

// Set the validation settings on the XmlReaderSettings object.

XmlReaderSettings settings = new XmlReaderSettings();

settings.ValidationType = ValidationType.Schema;

settings.Schemas.Add("urn:bookstore-schema", Path.Combine(path, "books.xsd"));

settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

// Create a validating reader that wraps the XmlNodeReader object.

XmlReader reader = XmlReader.Create(nodeReader, settings);

// Parse the XML file.

while (reader.Read());

}

// Display any validation errors.

private void ValidationCallBack(object sender, ValidationEventArgs e)

{

this.textBox1.Text = String.Format("Validation Error: {0}", e.Message);

}

}

}



 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Wonderful so it could be a deployment error. Very thanks!!!

 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Another thing. Where are the options: setAction & condition and what is their role

Hi & thanks

 
 
Ilya Tumanov





PostPosted: XML and the .NET Framework, xml validation Top

That's properties of files in VS project which are telling VS what to do with the file and when to do it. It's not device specific.



 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Thanks but I cannot find where to set that options can you explain me a where I can found it.

Thanks

 
 
Ilya Tumanov





PostPosted: XML and the .NET Framework, xml validation Top

Sure. VS comes with a great help system which is accessible via F1 button. Just hit F1, type “File properties” and you’ll find them in no time.



 
 
Giugio





PostPosted: XML and the .NET Framework, xml validation Top

Very thanks. Looking at the doc I believe this rule are contained in the assembly is correct

Ok another thing. This strategy to use XML for configuration of my app (Tomcat like) is the more appropriate Or .NET gives me some additional instrument to use

Sorry for the diozen of requests but I came from Java :)

Hi