如何将本地字符串变量插入 TextWriter Writeline 方法而不导致换行?

发布于 2024-12-23 09:33:00 字数 1640 浏览 0 评论 0原文

我试图将已解析的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2024-12-30 09:33:00

您正在做的

name.AppendLine(reader.ReadElementContentAsString())

事情是您应该添加新行。也许尝试一下

name.Append(reader.ReadElementContentAsString()) 

编辑:
尽管 name 似乎根本没有必要成为字符串生成器。也许只是把它变成一个字符串?

在这种情况下你可以这样做

name = reader.ReadElementContentAsString()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文