我有以下XML文件:
<?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>
我需要验证另一个XML文件。有没有办法可以检查另一个XML文件,如果它与我的文件具有相同的结构?
答案 0 :(得分:1)
您可以创建一个可能看起来像这样的xsd架构。 (我推荐@daryal的xml架构,但我会对xml元素施加一些约束。)
<xs:element name="SystemSettings">
<xs:complexType>
<xs:sequence>
<xs:element name="Workstation" minOccurs="0" maxOccurs="1" />
...
</xs:sequence>
</xs:complexType>
...
然后您可以使用此代码执行验证
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
}
}
}
}
}
答案 1 :(得分:1)
您需要创建xml scheme,然后使用XDocument.Validate方法检查xml是否符合方案
答案 2 :(得分:1)
您需要一个XSD文件来检查xml是否正确。如果您没有xsd文件,某些工具可能会创建xsd from xml,但您可能需要修改创建的xsd。
此answer描述了如何针对XSD文件验证c#中的xml文件。
<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>