Queries about validating xml with XmlReader

My experiences with XmlReader class have been kind of mixed right now. I do want to clarify some of the basic doubts which I have with validating the Xml data using XmlReader class.

  1. I know one can use the XmlReaderSettings class to set up some settings as to whether to turn on/off validation, setting the validation type, etc.. and create an XmlReader with the xml file and the settings object.
  2. I also know that you need to add all your schema files used for validation into the XmlSchemaSet class unless the schema is present inside the Xml document (inline schema)

There is a property in XmlSchemaValidationFlags class called as XmlSchemaValidationFlags.ProcessSchemaLocation

So, if I do have the location of the xsd file specified in the xml file given below, do I need to add the xsd file to the XmlSchemaSet class? I have kept the xsd file in the same dir as of the xml file.

<?xml version="1.0" encoding="utf-8"?> <value_tables xmlns="urn:values-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:values-schema values.xsd" > <table name="Calibrated"> <value num="0">No</value> <value num="1">Yes</value> </table> </value_tables>

Q2. Also, is it possible to retrieve the name of this file from the xml document rather than hardcoding it?

-------------Problems Reply------------

You'd normally need to supply a Resolver (google EntityResolver) which tells the parser where to find the externally referenced documents. Yes, you could let the resolver take the location info directly from the namespace URI (but this is not recommended; I think I remember some XBRL taxonomies use this approach IIRC).

There may or may not be default EntityResolver implementations that do simple things (like: look in a single directory or download from the URI as a http url), but in my experience I always had to code an EntityResolve one way or another. It is not a lot of work anyway

Just a word of advice. As the validator of XML input data, you definitively should control the schema source. How else would you know what you are validating against?

There are use cases where the schemaLocation attribute is useful. For instance, the Spring framework uses different URLs for the same namespace to let the user specify either default or version specific schema support for configuration files. But the URLs are always symbolic and points indirectly to controlled schema related resources.

This, however, does not apply if the use case is to gain schema related information about the elements and attributes in the XML document.

Category:c# Views:1 Time:2011-03-28
Tags: c# xml xsd

Related post

  • Example of reading well-formed and valid xml reading using xmlReader 2012-02-18

    I m looking for some good example of reading a well-formed and valid xml file (having external dtd attached to it) using XmlReader of c# Have googled it and gone through books but cannot find a good and well explained example. this is the XML file:

  • How does one test a file to see if it's a valid XML file before loading it with XDocument.Load()? 2008-12-17

    I'm loading an XML document in my C# application with the following: XDocument xd1 = new XDocument(); xd1 = XDocument.Load(myfile); but before that, I do test to make sure the file exists with: File.Exists(myfile); But... is there an (easy) way to te

  • Validating xml nodes, not the entire document 2009-04-03

    I'm working with some xml 'snippets' that form elements down the xml. I have the schema but I cannot validate these files because they are not complete xml documents. These snippets are wrapped with the necessary parent elements to form valid xml whe

  • Validating XML with XSD 2009-06-03

    I'm running into real difficulties validating XML with XSD. I should prefix all of this and state up front, I'm new to XSD and validation, so I'm not sure if it's a code issue or an XML issue. I've been to XML API hell and back with the bajillion dif

  • Single file non-validating xml parser/reader 2010-12-16

    I'm looking for a simple non-validating XML parser in either C or C++. Several years back I found one that was just a single file solution but I can't find it anymore. I'm after some links and suggested ones that are very small and lightweight ideall

  • How do I fix "Could not find schema-" error while validating XML against XSD? 2011-06-03

    I am working with a fairly complex third-party XSD format - XSDs refer to types in another XSD which refers to types in yet another XSD. I need to be able to validate XML against this format. I've been able to get this to validate using XML Spy but n

  • validating xml in ssis - memory problem 2011-09-12

    I am validating a xml with xsd in a SSIS package. i have a 1GB file, while validating that the memory consumption is going upto 3.5 gb. Is there anyway we can validate xml using xsd in SSIS with lesser memory (with c# validation or something). ------

  • validating xml document in oracle's java source 2011-10-17

    Trying to do subject. I'm trying to use xsd from file(schemasource = 1) and from clob (schemasource = 0). I have two xsd schemas common_types.xsd and migom.xsd. second includes first. The problem is that when I'm using common_types schema from file I

  • Validating XML String in VB.NET 2011-12-21

    Please note that I am very new to .NET and ASP.NET. I have a script that attempts to load XML text from a URL and I am having difficulty figuring out how to check to see if it is valid, normalized XML. I don't have a schema as there are several varia

  • How the heck can you edit valid XML in a webpage? 2008-10-28

    I've got to get a quick and dirty configuration editor up and running. The flow goes something like this: configuration (POCOs on server) are serialized to XML. The XML is well formed at this point. The configuration is sent to the web server in XEle

  • Struts Validation.xml - requiredif problem 2008-11-07

    I want my validation.xml to only check for a null if certain options are selected from a dropdown. So far I have <field property="empFDServiceStartDate" depends="requiredif, date"> <arg0 key="Service Start date" resource="false"/> <var

  • Is empty string valid XML? 2008-12-17

    Is '' a valid piece of XML? Can '' insert into oracle xblob column or XML column from MSSQL? --------------Solutions------------- It's not valid xml, but it can be inserted into an XML column in SQL Server 2005. I can't speak for Oracle, but I would

  • How we will enable front-end validation based on the xml in validation.xml? 2009-04-28

    i want to know that How we will enable front-end validation based on the xml in validation.xml? --------------Solutions------------- If you're using one of the themes that supports front-end validation (like css_xhtml), you should just have to set va

  • problem in multiple validation.xml for single action in struts2 2009-05-05

    I am working on struts2 application. I have a jsp page having 3 textfields. And I am validating each field through action-validation.xml file. Now I want if validation get fail at first field it should not check the other two fileds and result direct

  • action-validation.xml in struts2 (open different pages when validation fails at different fields in validation.xml) 2009-05-06

    I am working in struts2. I have two fields in my action-validation.xml. I want if validation get fails at first field it will go to some jsp page (say a.jsp) and if validation get fails at second field then it will go to another jsp (say b.jsp). As i

  • Is this a valid XML file? 2009-08-24

    I need to send some XML element to some other services and I want to ensure my XML file is of elegant format so that other people could use their XML parser to parse the XML file. For such kinds of XML file, is it elegant format, breaking any rules o

  • Is this a valid XML? 2009-09-05

    I have a feeling this XML is not valid, can someone please explain why? I think it has something todo this the dot i the element name? estate_price.price_suggestion Any thing else not valid about this XML? XML \\ <?xml version="1.0" encoding="UTF-

  • Valid XML, Valid schema. Where have I gone wrong? 2009-10-11

    Once upon a time I had a valid doctype and valid XML. I put the former on top of the latter, cntrl-s'd and behold: all was as all should be. Nowadays I have a valid schema (custom built for my still valid XML). I tried my same tried and true approach

  • Is xmlns="" a valid xml namespace? 2009-10-19

    Is "empty" a valid value for XML namespace? If yes what does it mean? I have the following XML code but I'm not sure to which namespace Field1 and Field2 elements belong to. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.127 (s). 11 q(s)