Link to home
Start Free TrialLog in
Avatar of socom1985
socom1985

asked on

C# XML validation without schema

Hello everybody

I'm looking for some easy way to check all the Tags in an XML file. To see if they are properly closed and so on. So check the whole structure of the XML file without a schema.

Could somebody give me an example how to do this? I'm a noob ;)

Avatar of saragani
saragani

You can do it by:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(string XmlFileContent);


or by:
xmlDoc.Load(string filename)

The load / LoadXML would throw an exception if the XML is not OK (Missing tags, invalid chars etc)
saragani is right - here is some code implementig that idea.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ValidateXML
{
    class Program
    {
        static void Main(string[] args)
        {
            //Valie
            string test1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><People><Person><Name>Dave</Name></Person></People>";
 
            //Invalid
            string test2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><People><Person><Name>Dave</Name></Person>";
 
            Console.WriteLine(IsValidXML(test1));
            Console.WriteLine(IsValidXML(test2));
 
            Console.Read();
        }
 
        static bool IsValidXML(string xmlContent)
        {
            try
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
 
                doc.LoadXml(xmlContent);
            }
            catch
            {
                return false;
            }
 
            return true;
        }
    }
}

Open in new window

Avatar of socom1985

ASKER

Thanks this helps.. But can you also tell me how to get the error? I want to see the location in the file..
If you want to see an error just try the attahced

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ValidateXML
{
    class Program
    {
        static void Main(string[] args)
        {
            //Valie
            string test1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><People><Person><Name>Dave</Name></Person></People>";
 
            //Invalid
            string test2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><People><Person><Name>Dave</Name></Person>";
 
            Console.WriteLine(IsValidXML(test1));
            Console.WriteLine(IsValidXML(test2));
 
            Console.Read();
        }
 
        static bool IsValidXML(string xmlContent)
        {
            //try
            //{
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
 
                doc.LoadXml(xmlContent);
            //}
            //catch
            //{
                //return false;
            //}
 
            return true;
        }
    }
}

Open in new window

I want to write the exeption into a string. I'm writing a tool that checks every XML File in a directory and shows all errors at the end.

I now see another problem with this method. I think it will only show me the first error in an xml. Its not a big problem but it would be great if you had another method for this problem.
Ok I see - well we could fix the current code sample to give you the error message as a string easily enough but yes it will only give you the first error.
I don't know of any easy way of finding all errors programmatically I'm afraid.  There should be somethng somewhere you can reuse.  If not you would have to write something yourself but that would be very difficult and tedious.
Hmm I see.. Ok. Then we should go with the current code sample and just the first error. Can I do a catch (Exception e) or what would you suggest?

Thanks..
If i do it with Exception e and e.Message I just get "Root element is missing." but I cant see which element..
ASKER CERTIFIED SOLUTION
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Its the Root element
Thanks for the code..
I changed a random closing tag. not the root element..
Sorry but the messages given by the parsing engine used by System.Xml.XmlDocument is out of my control I'm afraid!
Thanks. That's exactly it..