0

我有一个测试类,它测试两个生成的 xml 文件与一个已经生成且已知良好的文件匹配。比较写入磁盘的文件时测试通过。将仍在内存中的文件与从磁盘读取的已知良好文件进行比较时失败,因为预期的流长了 17 个字节。

测试班

[TestFixture]
class XmlExporterTest
{
    private const string ExpectedXMLFile = @"..\..\Projects\EXPECTED FILE.xml";

    private XmlExporter xmlExporter;

    [SetUp]
    public void SetUp()
    {
        // clean up from before, since we might want the info after the tests (if they fail for example)
        string[] filePaths = Directory.GetFiles(@"..\..\tmp");
        foreach (string f in filePaths)
        {
            File.Delete(f);
        }
    }

    // this always fails because the stream length is 17 bytes short
    [Test]
    public void TestExportToXMLStream()
    {
        // test write to stream
        using (Stream actualStream = xmlExporter.ExportToXML(true))
        {
            using (Stream expectedStream = new FileStream(ExpectedXMLFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
            {
                FileAssert.AreEqual(expectedStream, actualStream);
            }
        }
    }

    // this always passes
    [Test]
    public void TestExportToXMLFile()
    {
        const string actualXMLFile = @"..\..\tmp\project1.xml";
        xmlExporter.ExportToXML(actualXMLFile, true);
        FileAssert.AreEqual(ExpectedXMLFile, actualXMLFile);
    }
}

XML 导出类

public class XmlExporter
{
    public void ExportToXML(string filename, bool normalizeZoom)
    {
        iexINSPECTIONXPERT arr = CreateSerializableObject(normalizeZoom);

        //serialize to an XML file
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("xxx", "http://www.example.com");
        using (TextWriter tr2 = new StreamWriter(filename))
        {
            XmlSerializer sr2 = new XmlSerializer(typeof(Foo));
            sr2.Serialize(tr2, arr, ns);
        }
    }

    /// <summary>
    /// Exports to XML only returning a MemoryStream object.
    /// Note that the stream must be disposed of by the caller.
    /// </summary>
    /// <param name="normalizeZoom"></param>
    /// <returns></returns>
    public MemoryStream ExportToXML(bool normalizeZoom)
    {
        iexINSPECTIONXPERT arr = CreateSerializableObject(normalizeZoom);

        //serialize to an XML file
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("xxx", "http://www.example.com");
        var stream = new MemoryStream();
        XmlSerializer sr2 = new XmlSerializer(typeof(Foo));
        sr2.Serialize(stream, arr, ns);
        return stream;
    }
}

其他类

[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.example.com", ElementName = "Foo")]
public class Foo
{
    [XmlElementAttribute(Namespace = "", ElementName = "BAR")]
    public BAR fi;
}
4

1 回答 1

3

在写入文件和写入内存流时,您需要使用相同的编码,例如Encoding.Default在这两种情况下都使用。

文件:

using (TextWriter tr2 = new StreamWriter(filename, false, Encoding.Default))

记忆:

var memory = new MemoryStream();
var writer = new StreamWriter(memory, Encoding.Default);
sr2.Serialize(writer, arr, ns);
于 2012-07-25T16:49:04.237 回答