I have a very hard-coded XML reader below:
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;
}
}
}
}
}
It seems to be working fine for the most part in reading this XML page from my desktop. However, the Trace writer does not seem to be writing the same data as the console output. I have a snapshot of the console output here, and the bottom of the text file that the Trace is writing to here, and as you can see, the last few rows don’t match up.
For whatever reason, the text file is not receiving the same output as the console, even though they should be. Is there a reason anyone can see as to why the text file is being cut short here?
I was able to answer the question by replacing the Trace writer with a Streamwriter.
i.e.
for everything where:
would have been.
Thanks for the help for the few people that commented.