Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

xml schema validation

Avatar of Meir Rivkin
Meir RivkinFlag for Israel asked on
.NET ProgrammingC#
5 Comments1 Solution895 ViewsLast Modified:
i'm using configuration file and xsd file for my app installer.

basically, the config is used to perform pre/post installation procedures when upgrading application version:
ExistInstall - current installation
NewInstall - upgrade installation
PreInstall - prior installation/uninstallation
PostInstall - after installation/uninstallation

i.e.
PreInstall in ExistInstall element is procedure/s to take place before uninstall current installation
PostInstall in NewInstall element is procedure/s to take place after upgrade installation complete.

Q: i want to enforce minOccurs=0 and MaxOccurs=1 to the following elements:
NewInstall,ExistInstall,PreInstall,PostInstall,RollbackInstall,Procedures,PrePostProcedures
DailyRestart,InstallationDirectory

my schema looks valid but even if i duplicate one of the elements, the validation still returns true.

cheers




 

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace XMLValidatorNS
{
    public class XMLValidator
    {
        // Validation Error Message
        static List<string> errorMessage = new List<string>();

        /// <summary>
        /// Gets the error message.
        /// </summary>
        /// <value>The error message.</value>
        public static List<string> ErrorMessage
        {
            get { return XMLValidator.errorMessage; }
        }

        /// <summary>
        /// Validations the handler.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Xml.Schema.ValidationEventArgs"/> instance containing the event data.</param>
        public static void ValidationHandler(object sender,
                                             ValidationEventArgs args)
        {
            errorMessage.Add(args.Message);
        }


        /// <summary>
        /// Determines whether the specified XML is validated against schema.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <param name="schema">The schema.</param>
        /// <returns>
        /// 	<c>true</c> if the specified XML is validated; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValidated(string xml, string schema)
        {
            errorMessage.Clear();
            bool result = false;
            try
            {
                XDocument xdoc = XDocument.Load(xml);
                XmlSchemaSet schemas = new XmlSchemaSet();
                schemas.Add("", XmlReader.Create(schema));

                StringBuilder sb = new StringBuilder();
                xdoc.Validate(schemas, (o, e) =>
                {
                    errorMessage.Add(e.Message);
                }, true);

                result = true;
            }
            catch (Exception error)
            {
                errorMessage.Add(error.Message);
            }

            return result;
        }
    }
}
DeliveryStation.xsd
DeliveryStation.config
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir RivkinFlag of Israel imageFull stack Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers