我在下面有一个非常硬编码的 XML 阅读器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Diagnostics;
namespace XML
{
class Program
{
static void Main(string[] args)
{
// XML text reader stuff
XmlTextReader textReader = new XmlTextReader("H:\\User\\Desktop\\secLendingXML.cfm.xml");
//string[] Fed_Array = new string[] {"Actual Available to Borrow", "Outstanding Loans",
//"Par Submitted", "Par Accepted", "WTD Average Rate"};
int j = 0;
// Trace listener stuff.
Stream myFile = File.Create("H:\\User\\Desktop\\Fed_Sec_Lending.txt");
TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);
while (textReader.Read())
{
switch (textReader.NodeType)
{
case XmlNodeType.Element:
for (int i = 0; i < textReader.AttributeCount; i++)
{
textReader.MoveToAttribute(i);
switch (textReader.Name)
{
case "securityMaturityDate":
Trace.Write(textReader.Value + ",");
Console.Write(textReader.Value + ",");
break;
case "couponRate":
Trace.Write(textReader.Value + ",");
Console.Write(textReader.Value + ",");
break;
case "securityType":
Trace.Write(textReader.Value + ",");
Console.Write(textReader.Value + ",");
j = 0;
break;
case "value":
Trace.Write(textReader.Value + ",");
Console.Write(textReader.Value + ",");
j = j + 1;
if (j > 4)
{
Trace.Write(Environment.NewLine);
Console.Write(Environment.NewLine);
}
break;
}
}
break;
}
}
}
}
}
从我的桌面读取这个XML 页面的大部分工作似乎都很好。但是,跟踪编写器似乎没有写入与控制台输出相同的数据。我在这里有控制台输出的快照,以及 Trace 正在写入的文本文件的底部,如您所见,最后几行不匹配。
无论出于何种原因,文本文件都没有收到与控制台相同的输出,即使它们应该是。有没有人可以看到为什么文本文件在这里被缩短的原因?