如何将本地字符串变量插入 TextWriter Writeline 方法而不导致换行?
我试图将已解析的 XML 元素插入到 TextWriter WriteLine 的中间,而不会导致添加变量的换行符,但没有成功。该程序的目的是将 XML 文件转换为 X12 850 EDI 格式的 .txt 文件,并且我需要保留 WriteLine 方法指定的行。我不确定这是否是最有效的翻译方式,因为我的 C# 职业生涯才刚刚一周,但事实证明我使用 xslt 文件的其他尝试效率很低。任何指导将不胜感激。下面是我的翻译器的代码。
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~");
}
}
}
}
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~");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在做的
事情是您应该添加新行。也许尝试一下
?
编辑:
尽管 name 似乎根本没有必要成为字符串生成器。也许只是把它变成一个字符串?
在这种情况下你可以这样做
You are doing
Which you shouldexpect to add a new line. Maybe try just
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