简体   繁体   中英

Validating an XML file without an xsd file

I found a lot of great questions on this subject. Unfortunately the answers all say to use an xsd file. I made an xsd file from an xml file using xsd.exe. I copied code from here and pasted into Visual studio and I got an error on the first line.

Not wanted to spend time figuring out why it would not run I decided to code the validation myself.

Here are the two points I am using:

  1. Each left caret will have a right caret, so at the end of the file their will be an equal amount of left and right carets.

  2. At the end of the file, if I either take the amount of left carets, or the amount of right carets subtract one from the total (Because the header does not have a backslash) and divide the total by two, I get the number of slashes.

I am encountering some problems though.

  1. I am using string.count() This method also counts carets that are in the attributes (Which I do not want).

  2. I compute the expected number of backslashes when I am done reading the file. If the numbers do not match I write "The expected number of slashes do not match" But I don't know where it is in the file.

I can't think of a way to fix these problems at the moment.

Does anyone have a better way to validate an xml file without using an xsd file?

Take note that WELL FORMED XML is different to VALID XML.

XML that adheres to the XML standard is considered well formed, while XML that adheres to a DTD is considered VALID.

If you just want to check if XML is well formed try this:

try
{
    var file = "Your xml path";

    var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, XmlResolver = null };

    using (var reader = XmlReader.Create(new StreamReader(file), settings))
    {
        var document = new XmlDocument();
        document.Load(reader);
    }
}

catch (Exception exc)
{
    //show the exception here
}

PS: Well formedness of an XML is always a prerequisite of a valid XML.

Hope it helps!

When you say "caret", I think you must be talking about the "<" and ">" symbols, which in the XML world are usually referred to as "angle brackets".

When you talk about checking whether the carets match up, you are therefore talking about whether the file conforms to XML syntax. That's called "well-formedness checking" in the XML world. Validation is something different and deeper. You need a schema (either an XSD schema or some other kind) for validation, but for well-formedness checking all you need is an XML parser.

Don't try to implement the well-formedness checking yourself. (a) because it's not easy, (b) because parsers are readily available off-the-shelf, and (c) because you clearly don't have a very advanced understanding of the problem. Just run your file through an XML parser and it will do the job for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM