I am attempting to insert a parsed XML element into the middle of a TextWriter WriteLine without it causing a line break where the variable is added, but am having no success. The purpose of the program is to transform an XML file into a .txt file in X12 850 EDI format, and I need to preserve the lines as dictated by the WriteLine methods. I am not certain this is the most efficient way to make the translation, as I am only one week into my C# career, but my other attempts using xslt files have proven inefficient. Any guidance would be greatly appreciated. Below is the code for my translator.
class Class1
{
public static void Main()
{
StringBuilder name = new StringBuilder();
String xmlString = ("3.xml");
using (XmlReader reader = XmlReader.Create(new StreamReader(xmlString)))
{
reader.ReadToFollowing("Name");
name.AppendLine(reader.ReadElementContentAsString());
}
using (StreamWriter sw = new StreamWriter("test5.txt"))
{
sw.WriteLine("ISA*00* *00* *ZZ*daisywebstore *12*5016361200 *11213*1022*U*00400*000001649*0*P*>~");
sw.WriteLine("GS*PO*9254291001*5016361200*20111213*1022*1649*X*004010~");
sw.WriteLine("ST*850*0001~");
sw.WriteLine("BEG*00*SA*08272225944**20111213~");
sw.WriteLine("REF*DP*089~");
sw.WriteLine("DTM*002*20120104~");
sw.WriteLine("N1*ST*" + name + "~");
sw.WriteLine("N3*1400 S MC CARREN BLVD~");
sw.WriteLine("SN4*SPARKS*NV*894316301~");
sw.WriteLine("N1*RE**92*00103653341~");
sw.WriteLine("PO1*1*6*EA*33.28*TE*IN*081643211*UP*30039256056937~");
sw.WriteLine("PID*F****CO2 BB PISTOL $ 5693~");
sw.WriteLine("PO4*3*1*EA~");
sw.WriteLine("CTT*1~");
sw.WriteLine("AMT*1*199.68~");
sw.WriteLine("SE*16*0001~");
}
}
}
}
You are doing
name.AppendLine(reader.ReadElementContentAsString())
Which you shouldexpect to add a new line. Maybe try just
name.Append(reader.ReadElementContentAsString())
instead?
EDIT: Though it seems to be unnecessary for name to be a stringbuilder at all. Maybe just make it a string?
In which case you could just do
name = reader.ReadElementContentAsString()