通过xsd验证base64 zip xml文件

时间:2014-08-11 10:02:20

标签: c# xml validation xsd sharpziplib

我需要验证base64 zip xml文件。由SharpZipLib.Zip压缩。我创建用于验证的测试代码,但是当运行reader.Read()方法时,我有错误 - System.Xml.XmlException,'。',十六进制值0x00,是一个无效字符。第1行,第1位。

string xsdPath = @"c:\Test.xsd";
byte[] byteArray = Convert.FromBase64String(Resources.TestBase64);
using (Stream memInput = new MemoryStream(byteArray))
using (ZipInputStream input = new ZipInputStream(memInput))
{
    ZipEntry entry = input.GetNextEntry();

    byte[] newBytes = new byte[entry.Size];

    XmlSchemaSet sc = new XmlSchemaSet();
    StringReader stringReader = new StringReader(File.ReadAllText(xsdPath, Encoding.Default));

    // Add the schema to the collection.
    sc.Add(null, new XmlTextReader(stringReader));

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas = sc;
    settings.CheckCharacters = false;
    settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

    MemoryStream streamXml = new MemoryStream(newBytes);
    streamXml.Position = 0;
    StreamReader readerXml = new StreamReader(streamXml, Encoding.UTF8);

    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create(readerXml, settings);

    // Parse the file.  
    while (reader.Read())
    {


    }
}

private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
    var result = string.Format("Validation Error: {0} -- {1}", e.Message,e.Exception.LineNumber);
}

0 个答案:

没有答案