0

I want to use an xml document to store print strings that will be sent to an IPL printer along with some other data using C#.

Is there an attribute I can assign to the node that will ignore any xml syntax within that node and basically treat it as a string? For example I have a node below called string which will contain the print job that I want to send to the printer. The problem is the string has XML style tags within it which is causing formatting issues when viewed in visual studio. I am using C# serializer to read the xml tag and copy its contents to a string. What would be the best way to accomplish this task?

<string>
    <STX>R<ETX>
    <STX><ESC>C<SI>W565<SI>h<ETX>
    <STX><ESC>P<ETX>
    <STX>E3;F3<ETX>
    <STX>U1,LOGO;f3;o130,30;c2;h0,w0<ETX>
    <STX>H2,A1;f3;o130,220;c26;b0;h12;w12;d3,PART NO:<ETX>
    <STX>H3,B1;f3;o35,220;c26;b0;h8;w7;d3,Date Code<ETX>
    <STX>H4,C1;f3;o35,390;c26;b0;h8;w7;d3,Supplier Code<ETX>
    <STX>H5,D1;f3;o15,220;c26;b0;h8;w7;d3,lss 1<ETX>
    <STX>H6,E1;f3;o15,435;c26;b0;h8;w7;d3,ASSEMBLED IN USA<ETX>
    <STX>B7,CODEA;f3;o95,220;c0,3;w2;h55;r0;d0,10<ETX>
    <STX>H8,DATAA;f3;o130,350;c26;b0;h12;w12;d0;10<ETX>
    <STX>H9,DATAB;f3;o35,300;c26;b0;h8;w7;d0,8<ETX>
    <STX>H10,DATAC;f3;o35,510;c26;b0;h8;w7;d0,7<ETX>
    <STX>R<ETX>
    <STX><ESC>E3<CAN><ETX>
    <STX><ESC>F7<DEL>var0<ETX>
    <STX><ESC>F8<DEL>var1<ETX>
    <STX><ESC>F9<DEL>var2<ETX>
    <STX><ESC>F10<DEL>var3<ETX
    <STX><RS>1<US>1<ETX>
    <STX><ETB><FF><ETX>
  </string>
PL76
  • 83
  • 12
  • The main problem here is that the "xml" contained inside the `` node is not well-formed. Upload it to https://www.xmlvalidation.com and you will see an error `22: 5 Element type "ETX" must be followed by either attribute specifications, ">" or "/>".` And if I assume the unclosed `".` due to the fact that none of the nodes inside `` are closed! There's no way to deserialize this with any XML serializer. – dbc Jan 18 '18 at 19:15
  • Rather than XML, this looks to be HTML, so you will need to parse it with an HTML parser such as the [HTML Agility Pack](https://stackoverflow.com/q/846994/3744182) -- or, if your file format can be changed, fix it by modifying the "XML" so that the IPL printer instructions are embedded in a `CDATA` section as recommended in the [answer below](https://stackoverflow.com/a/48328598/3744182). That will make it well-formed XML. – dbc Jan 18 '18 at 19:16

1 Answers1

0

You can turn xml parsing off for your string by using CDATA.

<string>
  <![CDATA[
    <STX>R<ETX>
    [...]
    <STX><ETB><FF><ETX>
  ]]>
</string>

All content between the opening and closing CDATA tag will strictly be treated as string. For more see: What does CDATA in XML mean?

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • See also [How do you serialize a string as CDATA using XmlSerializer?](https://stackoverflow.com/q/1379888/3744182). – dbc Jan 18 '18 at 19:18