0

I have the following XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<Config version="Vs1.00.00"><!-- xmlns="urn:xmldata-schema">-->

<<SystemSettings>
    <Workstation></Workstation>
    <Company></Company>
    <InstallationDate></InstallationDate>
    <SupportContactLine1></SupportContactLine1>
    <SupportContactLine2></SupportContactLine2>
    <SupportContactLine3></SupportContactLine3>
    <UserFile></UserFile>
</SystemSettings>

<Settings>
    <StartUpWorkspace></StartUpWorkspace>
    <StartLanguage></StartLanguage>
    <ExportPath></ExportPath>
    <ImportPath></ImportPath>
    <BackUpRestorePath></BackUpRestorePath>
    <AdjHistoryPath></AdjHistoryPath>
    <TestHistoryPath></TestHistoryPath>
    <LogFilePath></LogFilePath>
</Settings>

<DefaultSettings>
    <DefaultSupportLine1></DefaultSupportLine1>
    <DefaultSupportLine2></DefaultSupportLine2>
    <DefaultSupportLine3></DefaultSupportLine3>
    <DefaultUserFile></DefaultUserFile>
    <DefaultStartUpWorkspace></DefaultStartUpWorkspace>
    <DefaultStartLanguage></DefaultStartLanguage>
    <DefaultWorkspacePath></DefaultWorkspacePath>
    <DefaultExportPath></DefaultExportPath>
    <DefaultImportPath></DefaultImportPath>
    <DefaultBackUpRestorePath></DefaultBackUpRestorePath>
    <DefaultAdjHistoryPath></DefaultAdjHistoryPath>
    <DefaultTestHistoryPath></DefaultTestHistoryPath>
    <DefaultLogFilePath></DefaultLogFilePath>
    <DefaultMasterPassword crypt="No"></DefaultMasterPassword>
    <DefaultLogger></DefaultLogger>
</DefaultSettings>

</Config>

I need to validate another XML file. Is there a way I can check another XML file if it has the same structure as my file here?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
klce
  • 220
  • 3
  • 14
  • I suppose there is a way using schema/XSD file template..I don't have any idea.."how-to"..as I am looking for the same too :) – beerBear Jan 31 '13 at 06:59

3 Answers3

1

You can create a xsd schema that possibly can look something like this. (I recommend @daryal's xml schema , but i would throw some constraints on the xml elements.)

<xs:element name="SystemSettings">
<xs:complexType>
  <xs:sequence>
    <xs:element name="Workstation" minOccurs="0" maxOccurs="1" />
    ...
  </xs:sequence>
</xs:complexType>

...

Then you can use this code to perform validation

using (var xsdStream = new MemoryStream(fXmlSchema))
        {
            xsdStream .Position = 0;
            using (var xsdReader = XmlReader.Create(xsdStream ))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
                settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
                settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
                settings.Schemas.Add(XmlSchema.Read(xsdReader , [performingValidationCallback]));

                using (var xmlStream = new MemoryStream(fXml))
                {
                    using (var xmlReader = XmlReader.Create(xmlStream, settings ))
                    {
                        try
                        {
                            xmlStream .Position = 0;
                            while (xmlReader .Read())
                            {
                            }
                        }
                        catch (XmlException e)
                        {
                            //VALIDATIONERROR
                        }
                    }
                }
            }
        }
Stian Standahl
  • 2,506
  • 4
  • 33
  • 43
1

You need to create xml scheme and then use XDocument.Validate method to check if the xml conform the scheme

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
1

You need an XSD file to check whether an xml is proper. If you do not have the xsd file, some tools may create xsd from xml, but you may need to modify created xsd.

This answer describes how to validate an xml file in c# against an XSD file.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Config">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="SystemSettings">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="Workstation"/>
              <xs:element type="xs:string" name="Company"/>
              <xs:element type="xs:string" name="InstallationDate"/>
              <xs:element type="xs:string" name="SupportContactLine1"/>
              <xs:element type="xs:string" name="SupportContactLine2"/>
              <xs:element type="xs:string" name="SupportContactLine3"/>
              <xs:element type="xs:string" name="UserFile"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Settings">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="StartUpWorkspace"/>
              <xs:element type="xs:string" name="StartLanguage"/>
              <xs:element type="xs:string" name="ExportPath"/>
              <xs:element type="xs:string" name="ImportPath"/>
              <xs:element type="xs:string" name="BackUpRestorePath"/>
              <xs:element type="xs:string" name="AdjHistoryPath"/>
              <xs:element type="xs:string" name="TestHistoryPath"/>
              <xs:element type="xs:string" name="LogFilePath"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="DefaultSettings">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="DefaultSupportLine1"/>
              <xs:element type="xs:string" name="DefaultSupportLine2"/>
              <xs:element type="xs:string" name="DefaultSupportLine3"/>
              <xs:element type="xs:string" name="DefaultUserFile"/>
              <xs:element type="xs:string" name="DefaultStartUpWorkspace"/>
              <xs:element type="xs:string" name="DefaultStartLanguage"/>
              <xs:element type="xs:string" name="DefaultWorkspacePath"/>
              <xs:element type="xs:string" name="DefaultExportPath"/>
              <xs:element type="xs:string" name="DefaultImportPath"/>
              <xs:element type="xs:string" name="DefaultBackUpRestorePath"/>
              <xs:element type="xs:string" name="DefaultAdjHistoryPath"/>
              <xs:element type="xs:string" name="DefaultTestHistoryPath"/>
              <xs:element type="xs:string" name="DefaultLogFilePath"/>
              <xs:element name="DefaultMasterPassword">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute type="xs:string" name="crypt"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element type="xs:string" name="DefaultLogger"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Community
  • 1
  • 1
daryal
  • 14,643
  • 4
  • 38
  • 54