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. General Programming
  3. C#
  4. c# and XML validation [modified]

c# and XML validation [modified]

Scheduled Pinned Locked Moved C#
questionxmlcsharpdatabasehelp
3 Posts 2 Posters 3 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.
  • M Offline
    M Offline
    mocasu
    wrote on last edited by
    #1

    Hi there, I have created an .xsd schema which I want to include in my project (Windows Service). First question is: what is the best way to include it? as a resource? or just by doing "Add existing item"? what is the difference(if any)? So far, I have added it as a resource (jobs.xsd). Ok, so now I want to validate an xml file, thus I am trying to do as follows:

    1. XmlSchemaSet xss = new XmlSchemaSet();
    2. //Add the schema to the collection
    3. xss.Add("http://www.w3.org/2001/XMLSchema",ClassLibrary1.Properties.Resources.jobs);
    4. XmlReaderSettings settings = new XmlReaderSettings();
    5. settings.ValidationType = ValidationType.Schema;
    6. settings.Schemas = xss;
    7. settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
    8. // Create the XmlReader object.
    9. XmlReader reader = XmlReader.Create(xmlPath, settings);
    10. // Parse the file.
    11. while (reader.Read()){};

    This code breaks on line 3, with a System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path) at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) at System.IO.Path.GetFullPathInternal(String path) at System.IO.Path.GetFullPath(String path) at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, String schemaUri) at ClassLibrary1.ReadXml.ValidateXMLAgainstXSD(String xmlPath) in c:\myproj\ClassLibrary1\GetXml.cs:line 95} How could I get around this problem? Cheers

    modified on Tuesday, March 3, 2009 6:57 AM

    G 1 Reply Last reply
    0
    • M mocasu

      Hi there, I have created an .xsd schema which I want to include in my project (Windows Service). First question is: what is the best way to include it? as a resource? or just by doing "Add existing item"? what is the difference(if any)? So far, I have added it as a resource (jobs.xsd). Ok, so now I want to validate an xml file, thus I am trying to do as follows:

      1. XmlSchemaSet xss = new XmlSchemaSet();
      2. //Add the schema to the collection
      3. xss.Add("http://www.w3.org/2001/XMLSchema",ClassLibrary1.Properties.Resources.jobs);
      4. XmlReaderSettings settings = new XmlReaderSettings();
      5. settings.ValidationType = ValidationType.Schema;
      6. settings.Schemas = xss;
      7. settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
      8. // Create the XmlReader object.
      9. XmlReader reader = XmlReader.Create(xmlPath, settings);
      10. // Parse the file.
      11. while (reader.Read()){};

      This code breaks on line 3, with a System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path) at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) at System.IO.Path.GetFullPathInternal(String path) at System.IO.Path.GetFullPath(String path) at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, String schemaUri) at ClassLibrary1.ReadXml.ValidateXMLAgainstXSD(String xmlPath) in c:\myproj\ClassLibrary1\GetXml.cs:line 95} How could I get around this problem? Cheers

      modified on Tuesday, March 3, 2009 6:57 AM

      G Offline
      G Offline
      Greg Chelstowski
      wrote on last edited by
      #2

      If you still have not figured it out, the .Add overload you're using expects the URi of your .xsd file, and not the file's contents as a string. It compiles, because there is not type mismatch, obviously. All you can do is stream the contents of your .xsd, then feed it to an XmlReader and then pass that XmlReader as your second parameter of .Add's next overload. That should help, hopefully.

      var question = (_2b || !(_2b));

      M 1 Reply Last reply
      0
      • G Greg Chelstowski

        If you still have not figured it out, the .Add overload you're using expects the URi of your .xsd file, and not the file's contents as a string. It compiles, because there is not type mismatch, obviously. All you can do is stream the contents of your .xsd, then feed it to an XmlReader and then pass that XmlReader as your second parameter of .Add's next overload. That should help, hopefully.

        var question = (_2b || !(_2b));

        M Offline
        M Offline
        mocasu
        wrote on last edited by
        #3

        Hi Greg, I think I should have been more precise. I knew I was passing the wrong data type to the .Add method. My problem was converting my string into a valid data type that the .Add will take. At the end I am doing as follows:

        StringReader sr = new StringReader(ClassLibrary1.Properties.Resources.jobs);
        XmlSchema schema = XmlSchema.Read(sr,null);
        sr.Close();
        //Add the schema to the collection
        xss.Add(schema);

        Anyway, thanks for your post!

        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