Validating an XML file with an external schema

Hi;

If I have an xml file and a schema file and I want to validate that the xml 
file matches the schema - is this the best way? (This code works, I just want 
to make sure I am doing this the best way.)

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation | 
XmlSchemaValidationFlags.ReportValidationWarnings;
XmlSchemaSet sc = new XmlSchemaSet();
sc.Add(null, schemaUrl);
settings.Schemas = sc;

reader = XmlReader.Create(url, settings);
while (reader.Read())
	;

-- 
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm


0
thielen (152)
6/30/2007 11:00:01 PM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
463 Views

Similar Articles

[PageSpeed] 48

David Thielen wrote:

> If I have an xml file and a schema file and I want to validate that the xml 
> file matches the schema - is this the best way? (This code works, I just want 
> to make sure I am doing this the best way.)
> 
> XmlReaderSettings settings = new XmlReaderSettings();
> settings.ValidationType = ValidationType.Schema;
> settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation | 
> XmlSchemaValidationFlags.ReportValidationWarnings;

You should only set the ProcessSchemaLocation flag if you are not 
providing the XML schema document(s) yourself. As you later add a schema 
yourself I think you do not want to set that flag.

What you have not shown is setting up a ValidationEventHandler on the 
settings object. That is necessary to have the various validation 
messages (warnings and errors) reported to your code, otherwise the 
first error will cause an exception and further errors are not reported.

> XmlSchemaSet sc = new XmlSchemaSet();
> sc.Add(null, schemaUrl);
> settings.Schemas = sc;

No need to create a schema set, you can simply do
   settings.Schemas.Add(null, schemaUrl);

> reader = XmlReader.Create(url, settings);
> while (reader.Read())
> 	;

Consider using the "using statement" e.g.
   using (XmlReader reader = XmlReader.Create(url, settings))
   {
     while (reader.Read()) ;
   }

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
0
mahotrash (1778)
7/1/2007 12:01:59 PM
thank you very much - that looks a lot better.

-- 
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




"Martin Honnen" wrote:

> David Thielen wrote:
> 
> > If I have an xml file and a schema file and I want to validate that the xml 
> > file matches the schema - is this the best way? (This code works, I just want 
> > to make sure I am doing this the best way.)
> > 
> > XmlReaderSettings settings = new XmlReaderSettings();
> > settings.ValidationType = ValidationType.Schema;
> > settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation | 
> > XmlSchemaValidationFlags.ReportValidationWarnings;
> 
> You should only set the ProcessSchemaLocation flag if you are not 
> providing the XML schema document(s) yourself. As you later add a schema 
> yourself I think you do not want to set that flag.
> 
> What you have not shown is setting up a ValidationEventHandler on the 
> settings object. That is necessary to have the various validation 
> messages (warnings and errors) reported to your code, otherwise the 
> first error will cause an exception and further errors are not reported.
> 
> > XmlSchemaSet sc = new XmlSchemaSet();
> > sc.Add(null, schemaUrl);
> > settings.Schemas = sc;
> 
> No need to create a schema set, you can simply do
>    settings.Schemas.Add(null, schemaUrl);
> 
> > reader = XmlReader.Create(url, settings);
> > while (reader.Read())
> > 	;
> 
> Consider using the "using statement" e.g.
>    using (XmlReader reader = XmlReader.Create(url, settings))
>    {
>      while (reader.Read()) ;
>    }
> 
> -- 
> 
> 	Martin Honnen --- MVP XML
> 	http://JavaScript.FAQTs.com/
> 
0
thielen (152)
7/1/2007 2:08:01 PM
Reply:

Similar Artilces:

Log files
I'm running exchange 2000 and have about 5 gig of log files. We aren't backing up the exchang because it's not that important. So how do I tell exchang to get rid of the really old log files? I would perform one Single online backup to purge your current log files then read http://support.microsoft.com/default.aspx?scid=kb;en-us;q258470 -- John Oliver, Jr. MCSE, MCT, CCNA, Exchange MVP Microsoft Certified Partner "Scott Smith" <scott@mithrax.com> wrote in message news:123ce01c3f59b$c29de6d0$a501280a@phx.gbl... > I'm running exchange 2000 and have about...

Lost Windows XP, Internet Explorer and supporting files
I somehow lost all my "Window" elements. Microsoft suggested that I try a "reinstall" which I did, but the problem was only 99% of the update downloaded and then it froze and now none of the Windows files work. Could not restore to earlier date as the download erased that option. I have been working with Microsoft and Dell to no avail. My main concern over wiping everything out and reloading from the Recovery Disk is that I will lose my "personal videos/pictures" and these can't be replaced. I've tried to download them on CD, uploadin...

Msgbox entry validation
The following Macro will not loop more than twice the error entries. Will someone help me correct it to loop indefinely? Thanks Sub Entry_validation() Dim Teststring As String Dim Inputstring As String Cells(1, 1) = "ABC" 'as given filename Cells(2, 1) = "DEF" 'as given filename Cells(3, 1) = "GHI" 'as given filename Cells(4, 1) = "JKL" 'as given filename errorloop: Inputstring = InputBox(prompt:="Enter Filename" & vbLf & "Enter abort to abort en...

Why the exported html file is not right?
When I save my excel worksheet into HTML and open it in my internet explorer 6.0, the display is not right. Some table border lines are missed, when I refresh it, it seems ok. Why? I need a html file that should be with no problem. Thanks. Junjie If you have a table with empty cells in your HTML like <TD></TD> then you will be missing some lines. When I create my HTML tables I always include &nbsp; if typing in my own HTML. If generating from Excel with my own macros I place an ASCII 160 character there so the cell will not be empty. I think a plain sp...

jpg files attached to sent mail
I've been told that recipients of my emails are getting two jpg files attached to my email. When opened they are almost blank. One has a white rectangle that looks like it could be a signature template. The other simply has a black dot. What are these files and how do i remove them so my mail recipients aren't bother by tyem? Thanks are you adding a signature? Do you use a header/footer in word? the small one is likely a bullet used in either the header or signature. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Need Help with Common Tasks? h...

Data Validation -> Validation list is larger than the cell width
I have a workbook that I've just added data validation to one cell. FWIW, the data validation is based on a named range. Here's the (perceived) problem. When I've set up data validation in the past, the selection is the same width as the cell. In this case, the list starts almost a full cell width to the left. What am I missing? Thanks, Barb Reinhardt Barb, In Excel2003 I can replicate what you describe under the following conditions: 1. the named range contains entries that are wider than the data validation cell 2. The data validation cell is r...

Importing Delimited Files
I'm trying to import data that is in a delimited format and I'm wondering if Access can do what I'm needing. The file starts out with a header section seperated by asterisks. After coming to a specific set of letters in the file there will be information that belongs under that header. This will be seperated by both * and ~. The file will continue on and have another header section followed by more information belonging with the second header. I'm needing to import several files into Access, sort them to match their appropriate header (information belonging to a cert...

Parsing XML in string variable?
I have a method that returns a string that contains XML, and I need to parse that string (ie. find specific keys). How can I do this? I found one that searches a Stream, but I don't know how to convert from string to stream. Thanks. hi vmi, you could use the System.Xml.XmlDocument class, simply make a new instance and use the LoadXml-Method (does not properly load string with encoding markers). You could also use the .NET 3.5 System.Xml.Linq.XElement. There are several utility classes to read from strings and streams and vice versa, e.g. System.IO.StringReader &am...

Money 2007 File Question
I have a full version of MS Money 2007. I installed a trial version of Money Deluxe and it changed my file. I uninstalled Money Deluxe and I cannot restore the original file. Can someone tell me how to restore the original file so that I can use it with Money 2007? Thanks in Advance, When you upgraded Money made a copy of your .mny file as .m16. Go find that, rename it as .mny and double click (assuming you have got M2007 installed) which will start Money. Note all transactions since you upgraded will have to be re-input! :-( -- Regards Bob Peel, Microsoft MVP - Money For unoff...

Make Excel opnening CSV files work properly again.
By company standard, I'm using an US edition of Windows XP pro. (sp2 all patches installed.) and Office 2003 SP1, again us edition. I work in holland so I would prefer using the dutch regional settings in my control panel. What frustates me: When I'm using US Regional settings in control panel, changing list seperator actually works and influences the way Excel opens CVS files. When i'm Dutch regional settings, changing the list seperator char does not influence the way Excel opnens CVS files, in effect we can't work with csv files at all. Please correct this as soon a...

looking for MFC files...
could someone send those files to me ..please.. cstring.cpp cstrarr.cpp cbytearr.cpp cptrarr.cpp cmapsp.cpp plex.cpp bashama2000@yahoo.com MFC source is installed on your machine as well. Take a look there instead. --- Ajay <bashama2000@gmail.com> wrote in message news:db12425c-a6d3-4937-aa50-682edc4eb3d0@h11g2000prf.googlegroups.com... > could someone send those files to me ..please.. > > cstring.cpp > cstrarr.cpp > cbytearr.cpp > cptrarr.cpp > cmapsp.cpp > plex.cpp > > bashama2000@yahoo.com On Mar 17, 3:22=A0pm, "Ajay Kalra" <ajayka.....

Deleting Custom Fields created in SQL Schema
Hi, I read in another thread that you cannot delete custom fields created in SQL Schemas. Will this be something available in the next version of SQL or CRM 5.0 or how or when can this be done. Concerned as we will have to create a large number of custom fields for an upcoming conversion and if we need to delete any then we are "hooped". Also, what is the limit to the number of custom fields you can add? Thanks! Shauna I am not sure on the next version of CRM if it will allow that or not. Currently there is no way to delete fields. The limit is not really a limit of CRM...

error LNK2001:unresolved external ymbol __imp__MessageBoxA@16
Hi everyone, I am trying to migrate current project from vc 5 ot vc 6. When I compile the project, and it looks fine. However, I got linking error message error LNK2001: unresolved external symbol __imp__MessageBoxA@16 error LNK2001: unresolved external symbol _<func name>@4 The function came from c file, and use export in DEF file I suppose MessageBox came from 'user32.dll' and this is stard lib, and it takes me 3 days but still can not figure out. Anybody has this problem before and how to solved it? Thanks advanced! Robert Goto Project->Sett...

Insert XML string into XML File: Part 2
Thanks to some good help from a previous post, I have been able to create well formed xml as part of a report logger app. However, I still have a small problem. When I add new xml to the log file, the new nodes are appended to the original file. What I really want is for the log file to grow as "report nodes" are added. Also, I am a bit concerned about performance, particularly as the file grows in size. I must write to an xml file (vs. a database) and am trying to understand the best way to do this. I prefer to not use "temp files" and would like to just modify the ...

Synchronizing .PST Files Between Multiple Computers
Hi Folks! What is the best way to keep two different .PST synchronized between two different computers? Currently, I have a Exchange account at work and a personal folder file with my personal calendar, contacts, etc. I, periodically, export the Exchange stuff to a .PST file and my personal .PST and bring it home to my computer there. I, periodically, export to the personal .PST file and then bring it back to work and import it there. Is there a better way to do this so that I don't have to keep importing/exporting all the time. Thank you very much! - Lawrence Lawrence &l...

Validating against a DTD
Hello All, I am trying to validate an xml file against a DTD. I went through a lot of examples especially: http://www.xmlforasp.net/codebank/util/srcview.aspx?path=../../codebank/System_Xml/XmlValidatingReader/Validator/validator.src&file=validator.cs&font=3 but its not working in my case. First of all, when i try to add my DTD file in XmlSchemaCollection object, it throws an error saying DocType element is expected. I am using NITF DTD for this and there is no DocType element in it. Any help will be appreciated. Thanks. VD You should not add DTDs to XmlSchemaCollection. XmlSchema...

XML Tree Structure
Hi Folks, I would like to take a XML document and write its data into a tree structure for a custom tree control. The document has two fields with one called task (a text field) and the other called tree_level (an integer field). Consider the document to be valid and well-formed. The tree_level is an index describing where a node will reside in a tree structure. A partial view of the XML document is as follows: <task>summary task</task> <tree_level>0</tree_level> <task>task1</task> <tree_level>1</tree_level> <task>task2</task&...

Validation dropdown list is not coming visible
Hi, I'm using XP and 2003. I have a problem that I have once found resolution, but now it came again and cannot remember what needs to be done. Here is the problem: There is a column where Validation is specified in right manner with error message in case of error. When selecting the cell - dropdown list is not coming visible. In case I write something wrong it gives error message. If I recall correctly - it was something to do with some kind of list. How can I change this to work properly? I tried to paste new column from another file where the column is working as I want...

Data Validation
Hi all I want to set data validation on a cell so that it will accept the following (and only the following) text strings: (1) "BR" (literally) or (2) "NT" (literally) or (3) "nA" where n is any positive integral numerical value including zero and A may take any of the values "L", "P", "T", "V" or "Y" or (4) "An" where n is any positive integral numerical value including zero and A may take either of the values "K" or "D". Is this possible, please, and if so how? thanks -- Return e...

[ANN] Liquid XML Studio Released
Announcing the release of Liquid XML Studio 2008 a Freeware XML Development Environment. Download available from www.liquid-technologies.com. Key features include - An Advanced Graphical Schema Editor - XML Editor, with auto-complete, syntax highlighting & validation - Web Service Browser, supports calling Soap services - XPath Expression Builder, visualise XPath expressions - Documentation Generator, HTML Docs from your XML schemas - Code Generation, C++, C#, .Net, Java, VB .Net & VB6 (trial) Liquid XML...XML the smart way! ...

trying to open old publisher files in Publisher 2003
I have old files from Windows version of publisher that I am grying to open up in Publisher 2003 Windows XP Anything below Publisher 95 will not open in 2003, all other versions will. Do you have Norton? How to use Office programs with the Norton AntiVirus Office plug-in http://support.microsoft.com/kb/329820/en-us -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Pat's problem" <Patsproblem@discussions.microsoft.com> wrote in message news:02D08C15-9386-48C6-B111-76AFAEF617FC@microsoft.com... >I have old file...

Outlook 2003 Archival
Hello, I have auto archive set on all users desktops on my LAN. I can not rely on the users to archive, or to save that file to their users folder. So I have all outlook 2003 on the LAN to run every 14 days, archive or delete mail greater than 6 months. For people that need mail greater than 6 months, I save the archive.pst file to a local server of mine this way it gets backed up nightly. I know this is not supported by MS as there can be corruption in the file, but ----- I really have no choice, it can not stay on their desktop (no backup), and for them to move the fi...

reinstall-validation problem
Hardware hit from lightning so new hd and video card and start over. All goes well with install using a slipstreamed XP-Pro SP3 disk but I notice it never asks for the Product Key during install yet it Activates itself during one of the boots and I think little of it until ... During the updates which followed (100+!!!) I notice Windows Defender doesn't seem to run or leave itself installed. As it is running on my laptop figured I just download it. When I run it, it wants to first do the Genuine Advantage bs which I let it, at which time it says "bogus". Ugh, g...

Can't access external hard drive after plugging it in to a mac
Is it possible to recover the files from an external hard drive after plugging it into a mac? Window Vista sees the drive and assigns it a drive letter, but I can't access any of the files and it keeps telling me to format the drive. I have files on it that I don't want to lose. Thanks, RJ On Wed, 25 Aug 2010 18:20:19 -0700 (PDT), RJ wrote: > Is it possible to recover the files from an external hard drive after > plugging it into a mac? Window Vista sees the drive and assigns it a > drive letter, but I can't access any of the files and it keeps telling &g...

Validation
Julie the same can be achieve without going thru the double drop down boxes. You could do it just using the combobox alone. Thank again Please stay in the ORIGINAL thread. The archives will thank you. -- Don Guillett SalesAid Software donaldb@281.com "JLong" <anonymous@discussions.microsoft.com> wrote in message news:034101c49c3b$889b5020$a501280a@phx.gbl... > Julie the same can be achieve without going thru the > double drop down boxes. You could do it just using the > combobox alone. Thank again ...