我使用Visual StudioXML工具创建了XSD。我使用下面的C#代码来验证XML并面对这个错误。
错误
元素没有声明为“http://www.w3.org/2000/09/XMLDSIG#:Signature”。
所以我的问题是如何修复它,因为在编辑模式下,XML是100%有效的?
谢谢你!
private void buttonValidateXML_Click(object sender, EventArgs e)
{
try
{
bool result = IsValidXml2(textBoxSignedXML.Text, textBoxXSDFile.Text, "");
rtbValidationResult.Text = result.ToString();
}
catch (Exception ex)
{
rtbValidationResult.Text = ex.Message;
}
}
public static bool IsValidXml2(string xmlFilePath, string xsdFilePath, string namespaceName)
{
var xdoc = XDocument.Load(xmlFilePath);
var schemas = new XmlSchemaSet();
schemas.Add(namespaceName, xsdFilePath);
bool result = true;
xdoc.Validate(schemas, (sender, e) =>
{
result = false;
});
return result;
}
<?xml version="1.0" encoding="utf-8"?>
<Envelope version="1">
<Deposit>
<ClientId>1234567890123</ClientId>
<Account>0045678</Account>
<Currency>USD</Currency>
<Total>5000.00</Total>
<SignedDate>2016-02-15</SignedDate>
<Cheques>
<Cheque>
<Images>
<Front>
SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
</Front>
<Back>
SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
1JtNV/9/rrffuO+h9bx////kfvABABAA
</Back>
</Images>
<MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
<Amount>465.22</Amount>
<PaymentDate>2016-02-15</PaymentDate>
<EmissionDate>2016-02-15</EmissionDate>
</Cheque>
<Cheque>
<Images>
<Front>
SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
</Front>
<Back>
SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
1JtNV/9/rrffuO+h9bx////kfvABABAA
</Back>
</Images>
<MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
<Amount>99999999999</Amount>
<PaymentDate>2016-02-15</PaymentDate>
<EmissionDate>2016-02-15</EmissionDate>
</Cheque>
</Cheques>
</Deposit>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>TVO2Gssf5TUdXFYG/PrHDyqYFqs=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>KwQInN03ywa0u0t4HedxgE1fOU7XYLQScKuQ6vdYoIZME5Hm5jpFeX2ORA2U+BO7JNjmFilTW05VntS3k98YCZhNXH9Iw/YEC1nw4JJLzygYbbCftkiY5v5+b494mQPryCtscwTtbziW6QilILSFDGmco2JopRfVe+qfdN/JyB1HXhUfApyNEsw/cJLj6aaz5ivN1sLFgAlikbwCNpF+mRnZY5u7/S8uT8WhEyK32EcatdjzKbP0PwnIlumhOpUMerWeLZ7neuJq6R/IuFgZ1Y5U6ppyuOjhtiHp4glC/uNUS/y7jMzG29thWBkEtSE9AcEt2IZ0HOEZE3kdFXufjA==</SignatureValue>
</Signature>
</Envelope>
XSD
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://example.com/ct-required"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime" >
<xs:import namespace="http://www.w3.org/2005/05/xmlmime"
schemaLocation="http://www.w3.org/2005/05/xmlmime"/>
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>
<xs:simpleType name="PNGPictureType"
xmime:expectedContentTypes="image/png">
<xs:restriction base="xs:base64Binary"/>
</xs:simpleType>
<xs:simpleType name="Money">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="13" />
<xs:fractionDigits value="2" />
<xs:minInclusive value="0.00" />
<xs:maxInclusive value="99999999999.99" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Currency">
<xs:annotation>
<xs:documentation>Currency Code: ISO 4217</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value="USD" />
<xs:enumeration value="UYU" />
<xs:minLength value="3" />
<xs:maxLength value="3" />
</xs:restriction>
</xs:simpleType>
<xs:element name="Envelope">
<xs:complexType>
<xs:sequence>
<xs:element name="Deposit" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="ClientId" type="xs:unsignedLong" />
<xs:element name="Account" type="xs:unsignedLong" />
<xs:element name="Currency" type="Currency" />
<xs:element name="Total" type="Money" />
<xs:element name="SignedDate" type="xs:date" />
<xs:element name="Cheques">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Cheque">
<xs:complexType>
<xs:sequence>
<xs:element name="Images">
<xs:complexType>
<xs:sequence>
<xs:element name="Front" type="PNGPictureType" />
<xs:element name="Back" type="PNGPictureType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="MicrCodeCmc" type="xs:string" />
<xs:element name="Amount" type="Money" />
<xs:element name="PaymentDate" type="xs:date" />
<xs:element name="EmissionDate" type="xs:date" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element xmlns:q1="http://www.w3.org/2000/09/xmldsig#" ref="q1:Signature" />
</xs:sequence>
<xs:attribute name="version" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
更新#1
我试过不同的方法,但都不开心。
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212 file:///C:/Temp/xmldsig-core-schema.xsd"/>
即使我用这种方法也不快乐。
public static bool IsValidXml1(string xmlFilePath, string xsdFilePath, string namespaceName)
{
XDocument xdoc = null;
var settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
settings.ProhibitDtd = false;
try
{
using (XmlReader xr = XmlReader.Create(xmlFilePath, settings))
{
xdoc = XDocument.Load(xr);
var schemas = new XmlSchemaSet();
schemas.Add(namespaceName, xsdFilePath);
schemas.Add(@"http://www.w3.org/2000/09/xmldsig#", @"D:\Temp\xmldsig-core-schema.xsd");
xdoc.Validate(schemas, null);
return true;
}
}
catch (XmlSchemaValidationException ex)
{
// throw;
}
return false;
}
如果您不想将任何内容更改为xsd或xml,请执行以下操作:
>
(可选)从w3站点下载xsd并保存到本地磁盘。W3站点非常慢,因为世界各地的很多软件都在不断地请求这些模式。如果您将直接使用XSD-您将经常以超时失败。某些验证工具已经在本地缓存了这样的架构,但.NET验证器没有。
public static bool IsValidXml1(string xmlFilePath, string xsdFilePath, string namespaceName)
{
XDocument xdoc = null;
var settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
try
{
using (XmlReader xr = XmlReader.Create(xmlFilePath, settings))
{
xdoc = XDocument.Load(xr);
var schemas = new XmlSchemaSet();
schemas.Add(namespaceName, xsdFilePath);
using (var fs = File.OpenRead(@"D:\Temp\xmldsig-core-schema.xsd"))
using (var reader = XmlReader.Create(fs, new XmlReaderSettings() {
DtdProcessing = DtdProcessing.Ignore // important
})) {
schemas.Add(@"http://www.w3.org/2000/09/xmldsig#", reader);
}
xdoc.Validate(schemas, null);
return true;
}
}
catch (XmlSchemaValidationException ex)
{
// throw;
}
return false;
}
您必须使用XMLReader
而不是直接添加该架构,因为如果您直接添加(如update 2中)-它将无法解析DTD块,因为当您使用url(或文件路径)将XMLSchema
添加到XMLSchemaSet
时-它将使用XmlReaderSettings
并使用DTDProcessing=DTDProcessing.禁止
读取该文件。我们需要将其更改为dtdprocessing.ignore
或dtdprocessing.parse
。在此之后,您的验证方法对于目标xsd和xml文件将很好地工作,没有任何更改(并且在xml与xsd不匹配的情况下将正确地失败)。
我对XML非常陌生,我面临验证问题,如果我的XML和XSD文件结构良好,我也希望得到一些反馈。 在根据XSD验证XML时,我经常遇到错误- 我正在windows上使用程序XML复制编辑器。我还针对XSD尝试了一个在线验证器XML,但出现了这个错误。https://www.freeformatter.com/xml-validator-xsd.html I get error Src resolve
我正面临XML声明的问题。我想从XML文件中省略XML声明。为此,我在XSLT文件中编写了下面的代码。
问题内容: 我包括以下标题: 我也尝试使用 之前,但这也无济于事。 我尝试使用并将其作为第二个参数传递,但我不断收到此错误消息: 错误:未声明“ F_SETPIPE_SZ”(此函数中的首次使用) 我实际上发现我不需要它,但是我很好奇为什么我不能使用它。 谢谢。 因此,感谢Chrono Kitsune,这是解决方案: 在任何包含之前。 问题答案: 因此,有了Chrono Kitsune,这就是解决方
Section Contents 基本内容 具体细节
下面是XSD。 下面是xml文件。 当尝试使用http://www.utilities-online.info/xsdvalidation验证它时,我得到以下错误 第2行62:org.xml.sax.SAXParseException;行号:2;列号:62;CVC-ELT.1:找不到元素“Message”的声明。
问题内容: 我有一个要解析的xml,进行了一些更改并将其保存到新文件中。它有我要保留的声明。当我保存新文件时,我失去了一点。我如何保留它?这是我的代码: 问题答案: 您可以将关键字参数传递给: