Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Validate an XmlDocument object

Validate an XmlDocument object

Scheduled Pinned Locked Moved ASP.NET
xmlquestiondatabaseannouncement
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Gathogo
    wrote on last edited by
    #1

    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

    C B 2 Replies Last reply
    0
    • J John Gathogo

      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

      C Offline
      C Offline
      Christian Flutcher
      wrote on last edited by
      #2

      XMLDocument.LoadXML supports loading from a XMLReader 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.

      J 1 Reply Last reply
      0
      • J John Gathogo

        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

        B Offline
        B Offline
        Brij
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • C Christian Flutcher

          XMLDocument.LoadXML supports loading from a XMLReader 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.

          J Offline
          J Offline
          John Gathogo
          wrote on last edited by
          #4

          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.

          C 1 Reply Last reply
          0
          • J John Gathogo

            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.

            C Offline
            C Offline
            Christian Flutcher
            wrote on last edited by
            #5

            Use the other overload which takes a TextReader instance. Your code would be

            StringReader stringReader = new StringReader(YourXMLString);
            XMLReader reader = XMLReader.Create(stringReader,settings);
            XMLDocument document = new XMLDocument();
            document.LoadXML(reader);

            StringReader is a subclass of TextReader.

            J 1 Reply Last reply
            0
            • C Christian Flutcher

              Use the other overload which takes a TextReader instance. Your code would be

              StringReader stringReader = new StringReader(YourXMLString);
              XMLReader reader = XMLReader.Create(stringReader,settings);
              XMLDocument document = new XMLDocument();
              document.LoadXML(reader);

              StringReader is a subclass of TextReader.

              J Offline
              J Offline
              John Gathogo
              wrote on last edited by
              #6

              Your proposal is very correct. Though with my mXmlDocument, all I did was: XmlReader mXmlReader = XmlReader.Create(new StringReader(mXmlDocument.OuterXml), mReaderSettings)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups