我在下面有一个非常硬编码的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;
}
}
}
}
}
从我的桌面阅读this XML页面,大部分工作似乎都很好。但是,Trace编写器似乎没有编写与控制台输出相同的数据。我有一个控制台输出here的快照,以及跟踪写入here的文本文件的底部,正如您所看到的,最后几行不匹配。
无论出于何种原因,文本文件都没有收到与控制台相同的输出,即使它们应该是。是否有人可以看到为什么文本文件在这里缩短了?
答案 0 :(得分:0)
我能够通过用Streamwriter替换Trace编写器来回答这个问题。
using (StreamWriter writer = new StreamWriter("H:\\myName\\Desktop\\Fed_Sec_Lending.txt"))
即
string textvalmatd = textReader.Value;
writer.Write(textvalmatd + ",");
所有地方:
Trace.Write(textvalmatd + ",");
本来可以。
感谢为评论的少数人提供的帮助。