Validate an XmlDocument object
-
I have some data passed to a function as a string like: "<?xml version=\"1.0\" encoding=\"utf-16\"?><Patient xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\\"><FirstName>Joe</FirstName><LastName>Klink</LastName></Patient>" I load this data into an XmlDocument object as follows: mXmlDocument.LoadXml(var) - var has the xml string above how can I validate this XmlDocument object against an existing schema. Background: I can easily validate an xml file against a schema if am reading it from a physical file using XmlReader. My challenge is when am dealing with a loaded XmlDocument. Thanks
-
I have some data passed to a function as a string like: "<?xml version=\"1.0\" encoding=\"utf-16\"?><Patient xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\\"><FirstName>Joe</FirstName><LastName>Klink</LastName></Patient>" I load this data into an XmlDocument object as follows: mXmlDocument.LoadXml(var) - var has the xml string above how can I validate this XmlDocument object against an existing schema. Background: I can easily validate an xml file against a schema if am reading it from a physical file using XmlReader. My challenge is when am dealing with a loaded XmlDocument. Thanks
XMLDocument.LoadXML
supports loading from aXMLReader
instance. XMLReader can be loaded from a stream. You can make a stream from the XML string you have. So if you know how to validate on a XMLReader, I think it can be applied here. -
I have some data passed to a function as a string like: "<?xml version=\"1.0\" encoding=\"utf-16\"?><Patient xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\\"><FirstName>Joe</FirstName><LastName>Klink</LastName></Patient>" I load this data into an XmlDocument object as follows: mXmlDocument.LoadXml(var) - var has the xml string above how can I validate this XmlDocument object against an existing schema. Background: I can easily validate an xml file against a schema if am reading it from a physical file using XmlReader. My challenge is when am dealing with a loaded XmlDocument. Thanks
For validating a XMLDocument object you have to follow the below steps: first create the XmlReaderSettings object XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add("urn:xmlns:25hoursaday-com:my-bookshelf", xsd); settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); settings.ValidationType = ValidationType.Schema; Then create the validating XmlReader object and the XmlDocument object. XmlDocument mXmlDocument = new XmlDocument(); mXmlDocument.Load(XmlReader.Create(document, settings)); Now validate the XMLdocument mXmlDocument.Validate(new ValidationEventHandler(ValidationCallback), element.ParentNode);
Cheers!! Brij
-
XMLDocument.LoadXML
supports loading from aXMLReader
instance. XMLReader can be loaded from a stream. You can make a stream from the XML string you have. So if you know how to validate on a XMLReader, I think it can be applied here.Thanks man. I had a feeling this is the work around I would need to use. The thing I have to focus on now is how to 'make a stream from the XML string'. I am googling my way to it but if you have some quick pointers I will be glad to hear. Thanks once again.
-
Thanks man. I had a feeling this is the work around I would need to use. The thing I have to focus on now is how to 'make a stream from the XML string'. I am googling my way to it but if you have some quick pointers I will be glad to hear. Thanks once again.
Use the other overload which takes a
TextReader
instance. Your code would beStringReader stringReader = new StringReader(YourXMLString);
XMLReader reader = XMLReader.Create(stringReader,settings);
XMLDocument document = new XMLDocument();
document.LoadXML(reader);StringReader
is a subclass ofTextReader
. -
Use the other overload which takes a
TextReader
instance. Your code would beStringReader stringReader = new StringReader(YourXMLString);
XMLReader reader = XMLReader.Create(stringReader,settings);
XMLDocument document = new XMLDocument();
document.LoadXML(reader);StringReader
is a subclass ofTextReader
.Your proposal is very correct. Though with my mXmlDocument, all I did was: XmlReader mXmlReader = XmlReader.Create(new StringReader(mXmlDocument.OuterXml), mReaderSettings)