0

生成された xml ファイルが既に生成されており、適切であることがわかっているファイルと一致することを 2 つテストするテスト クラスがあります。ディスクに書き込まれたファイルを比較すると、テストに合格します。予想されるストリームが 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 に答える