C# 通过xsd验证base64 zip xml文件

C# 通过xsd验证base64 zip xml文件,c#,xml,validation,xsd,sharpziplib,C#,Xml,Validation,Xsd,Sharpziplib,我需要验证base64 zip xml文件。用SharpZipLib.Zip拉链。我创建了用于验证的测试代码,但当运行reader.Read方法时,我发现错误-System.Xml.xmleexception,'.',十六进制值0x00是无效字符。第1行,位置1 string xsdPath = @"c:\Test.xsd"; byte[] byteArray = Convert.FromBase64String(Resources.TestBase64); using (Stream memI

我需要验证base64 zip xml文件。用SharpZipLib.Zip拉链。我创建了用于验证的测试代码,但当运行reader.Read方法时,我发现错误-System.Xml.xmleexception,'.',十六进制值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);
}

如果没有实际的xml,我们将无法帮助您。XML中似乎有一些无效字符。问题是缺少:input.ReadnewBytes,0,newBytes.Length@zrabzdn首先为什么要复制输入流?为什么不直接在StreamReader中使用该流?@Luaan这是一种测试方法,重构后可以在StreamReader中使用