Validating XML Document using XSD in WCF Service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arulmanoj
    New Member
    • Mar 2009
    • 34

    Validating XML Document using XSD in WCF Service

    I writing a WCF service to get a xml file as input and validate it against a schema. I have done the validation part. But I don't know how to write method in WCF service to get xml file as input.

    Below i have given code i have written in WCF service.

    Code:
      public string CheckXMLData(XmlDocument xmlDoc)
            {
                TheXsd = "Customer.xsd";
    
                //load schema 
                XmlSchemaCollection xsc = new XmlSchemaCollection();
                xsc.Add("Customer", TheXsd);
                Validate(xmlDoc, xsc);
                return " Validation Result";
            }
    
            public string Validate(XmlDocument xmlDoc, XmlSchemaCollection xsc)
            {
                XmlTextReader reader = null;
                XmlValidatingReader vreader = null;
    
                //reader = new XmlTextReader(xmlDoc);
                reader = new XmlTextReader(xmlDoc.ToString());
                vreader = new XmlValidatingReader(reader);
                vreader.Schemas.Add(xsc);
                vreader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                try
                {
                    while (vreader.Read()) { }
                }
                catch
                {
                    return Output = "XML Document is not well-formed.";
                }
                vreader.Close();
                return Output;
            }
    
            public void ValidationCallBack(object sender, ValidationEventArgs args)
            {
                Outcome = "<font color=\"red\">Failed:</font>";
                Output += "Validation error: <font color=\"red\">" + args.Message + "</font><br>";
            }
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Originally posted by Arulmanoj
    Hi,

    I writing a WCF service to get a xml file as input and validate it against a schema. I have done the validation part. But I don't know how to write method in WCF service to get xml file as input.

    Below i have given code i have written in WCF service.

    Thanks

    Manoj

    Code:
      public string CheckXMLData(XmlDocument xmlDoc)
            {
                TheXsd = "Customer.xsd";
    
                //load schema 
                XmlSchemaCollection xsc = new XmlSchemaCollection();
                xsc.Add("Customer", TheXsd);
                Validate(xmlDoc, xsc);
                return " Validation Result";
            }
    
            public string Validate(XmlDocument xmlDoc, XmlSchemaCollection xsc)
            {
                XmlTextReader reader = null;
                XmlValidatingReader vreader = null;
    
                //reader = new XmlTextReader(xmlDoc);
                reader = new XmlTextReader(xmlDoc.ToString());
                vreader = new XmlValidatingReader(reader);
                vreader.Schemas.Add(xsc);
                vreader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                try
                {
                    while (vreader.Read()) { }
                }
                catch
                {
                    return Output = "XML Document is not well-formed.";
                }
                vreader.Close();
                return Output;
            }
    
            public void ValidationCallBack(object sender, ValidationEventArgs args)
            {
                Outcome = "<font color=\"red\">Failed:</font>";
                Output += "Validation error: <font color=\"red\">" + args.Message + "</font><br>";
            }
    To create a WCF take a look at this.Creating, Configuring and Consuming a WCF service in IIS 7 In mean time please explain what exactly is going wrong? What do you really need?

    Comment

    • Arulmanoj
      New Member
      • Mar 2009
      • 34

      #3
      Hi Monomachus,

      I want to validate a xml file and against a xml schema using a WCF service in C#. I will be sending the XML file as a XML document to the WCF service. The service will validate the XML file against the predefined scheme file in the service. Here I am not able to write a method to get XML file as input from the client who is consuming the service. I am using XmlTextReader and XmlValidatingRe ader to read and validate the XML file.

      Thanks
      Manoj

      Comment

      • Arulmanoj
        New Member
        • Mar 2009
        • 34

        #4
        Originally posted by Monomachus
        To create a WCF take a look at this.Creating, Configuring and Consuming a WCF service in IIS 7 In mean time please explain what exactly is going wrong? What do you really need?
        Hi Monomachus,

        I want to validate a xml file and against a xml schema using a WCF service in C#. I will be sending the XML file as a XML document to the WCF service. The service will validate the XML file against the predefined scheme file in the service. Here I am not able to write a method to get XML file as input from the client who is consuming the service. I am using XmlTextReader and XmlValidatingRe ader to read and validate the XML file.

        Thanks
        Manoj

        Comment

        Working...