我有以下Xml,我想针对Xsd Schema进行验证(如下)。 我试图使用下面给出的C#函数针对给定的Xsd Schema验证Xml。我收到此通用类错误消息“根级别的数据无效。第1行,位置1”。这发生在“while(vr.Read())”。
您能否解释一下这个问题的原因和解决方法,或者我可以解决这些类型的一般错误。
从我的Xsd架构中可以看出它也引用了其他子xsd文件。我想知道它是否与错误有关。
温馨的问候
XML文件
<?xml version="1.0" encoding="utf-8"?>
<n1:Form109495CTransmittalUpstream xmlns="urn:us:gov:treasury:irs:ext:aca:air:6.2" xmlns:irs="urn:us:gov:treasury:irs:common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n1="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage" xsi:schemaLocation="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd">
<Form1094CUpstreamDetail recordType="C" lineNum="0">
<--MORE XML TAGS HERE -->
</Form1094CUpstreamDetail>
</n1:Form109495CTransmittalUpstream>
XSD架构
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:irs="urn:us:gov:treasury:irs:common"
xmlns:air6.2="urn:us:gov:treasury:irs:ext:aca:air:6.2"
targetNamespace="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
<xsd:annotation>
<xsd:appinfo>
<release>ACA Release 6.2</release>
</xsd:appinfo>
<xsd:documentation>Form-1094C (Issuer) Transmittal and Form 1095C - Transmittal of Health Insurance Coverage Statements
<VersionNum>5.2</VersionNum>
<VersionEffectiveBeginDt>2015-01-06</VersionEffectiveBeginDt>
</xsd:documentation>
</xsd:annotation>
<!-- ===== Imports ===== -->
<xsd:import namespace="urn:us:gov:treasury:irs:common" schemaLocation="common-IRS-CAC.xsd"/>
<xsd:import namespace="urn:us:gov:treasury:irs:ext:aca:air:6.2"
schemaLocation="ext-IRS-EXT-ACA-AIR-6.2.xsd"/>
<xsd:element name="Form109495CTransmittalUpstream" type="Form109495CTransmittalUpstreamType">
<xsd:annotation>
<xsd:documentation>
<Component>
<DictionaryEntryNm>Form109495C Transmission Upstream</DictionaryEntryNm>
<MajorVersionNum>1</MajorVersionNum>
<MinorVersionNum>1</MinorVersionNum>
<VersionEffectiveBeginDt>2015-01-06</VersionEffectiveBeginDt>
<VersionDescriptionTxt>Initial Version</VersionDescriptionTxt>
<DescriptionTxt>The elements associated with 1094C data generated EOY report</DescriptionTxt>
</Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="Form109495CTransmittalUpstreamType">
<xsd:annotation>
<xsd:documentation>
<Component>
<DictionaryEntryNm>Form109495C Transmission Upstream Type</DictionaryEntryNm>
<MajorVersionNum>1</MajorVersionNum>
<MinorVersionNum>1</MinorVersionNum>
<VersionEffectiveBeginDt>2014-11-05</VersionEffectiveBeginDt>
<VersionDescriptionTxt>Initial Version</VersionDescriptionTxt>
<DescriptionTxt>Transmission type for 1094C forms upstream data generated EOY report</DescriptionTxt>
</Component>
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="air6.2:Form1094CUpstreamDetail" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
以下是C#验证功能:
/// <SUMMARY>
/// This method validates an xml string against an xml schema.
/// </SUMMARY>
/// <PARAM name="xml">StringReader containing xml</PARAM>
/// <PARAM name="schemaNamespace">XML Schema Namespace</PARAM>
/// <PARAM name="schemaUri">XML Schema Uri</PARAM>
/// <RETURNS>bool</RETURNS>
public bool ValidXmlDoc(StringReader xml,
string schemaNamespace, string schemaUri)
{
// Continue?
if(xml == null || schemaNamespace == null || schemaUri == null)
{
return false;
}
isValidXml = true;
XmlValidatingReader vr;
XmlTextReader tr;
XmlSchemaCollection schemaCol = new XmlSchemaCollection();
schemaCol.Add(schemaNamespace, schemaUri);
try
{
// Read the xml.
tr = new XmlTextReader(xml);
// Create the validator.
vr = new XmlValidatingReader(tr);
// Set the validation tyep.
vr.ValidationType = ValidationType.Schema;
// Add the schema.
if(schemaCol != null)
{
vr.Schemas.Add(schemaCol);
}
// Set the validation event handler.
vr.ValidationEventHandler +=
new ValidationEventHandler(ValidationCallBack);
// Read the xml schema.
while(vr.Read())
{
}
vr.Close();
return isValidXml;
}
catch(Exception ex)
{
this.ValidationError = ex.Message;
return false;
}
finally
{
// Clean up...
vr = null;
tr = null;
}
}
答案 0 :(得分:1)
AFAIK XmlValidatingReader已过时。我认为你会更好地使用XmlReader。为了控制URI解析(如果模式引用了其他方案作为相对路径),您可以通过继承XmlUrlResolver并重写ResolveUri方法来创建自己的XML URL解析器类。然后将其分配给XmlReaderSettings类的XmlResolver成员。但我希望事情不会因此而恶化。:)希望。:)这是我可以建议的示意代码:
XmlTextReader reader = new XmlTextReader(pathToXSD);
XmlSchema schema = XmlSchema.Read(reader, ValidationEventHandler);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
settings.Schemas.Add(schema);
using (StringReader xmlStream = new StringReader(xml))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream, settings))
{
while (xmlReader.Read()) { }
}
}