question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked MarkusFreitag-0088 commented

C# Save as XML ASCII characters control characters

Hello,

 <?xml version="1.0" encoding="utf-8"?>
 <root>
   <EANCode>[)> 06 11V6255595 P605555501 16SV Q2330   </EANCode>
 </root>

135934--code1.png


ASCII29 ASCII9 and so one.

I want to save special characters inside a XML file.
Works not.

Why and what can I do ?



I know < is special.

Write -->
Read -->

Rename it? Replace it?
What is the best way for this issue? Do you have a sample for me. Write and read.

Thanks.

Best regards


dotnet-csharpdotnet-adonet
-code1.png (67.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Typically, special XML characters are encoded or placed in a CDATA section.


   <greaterThan>&gt;</greaterThan>
   <lessThan>&lt;</lessThan>
   <quote>&quot;</quote>
   <apostrophe>&apos;</apostrophe>
   <ampersand>&amp;</ampersand>
   <![CDATA["'<>&]]>

0 Votes 0 ·

Hello,
first thanks.
Is not so clear for me. Can you make a sample to write and read it inside a variable?

 MatNumber="[)> 06 11V6255595"
 ProductionNumber ="P605555501 16SV Q2330"

Is string with special characters.

How I must save it? How I can read it again in a variable?

Thanks for a sample.

0 Votes 0 ·
cooldadtx avatar image
1 Vote"
cooldadtx answered MarkusFreitag-0088 commented

Two approaches come to mind. The first approach is to encode the string value. Base64 is the most common but you could also use hex encoding. Just be sure to always encode/decode so there is no confusion.

var someValue = "Hello>There";
var encodedValue = Convert.ToBase64String(Text.Encoding.UTF8.GetBytes(someValue));


The alternative is to wrap the value in a CDATA. CDATA allows you to put non-XML stuff into XML. It is most often used when trying to put things like formatted code into XML. However some characters can still cause issues so personally I would base64 encode the value.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

var encodedValue = Convert.ToBase64String(Text.Encoding.UTF8.GetBytes(someValue));

MatNumber="[)> 06 11V6255595"

I use the string, convert it and save as XML, Right?

When I read the element value or attribute, how do I convert it then? Can you make please a sample?

0 Votes 0 ·
cooldadtx avatar image cooldadtx MarkusFreitag-0088 ·

When writing out the XML (assuming you are doing this by hand) then convert to base64. When reading convert from base64.

var MatNumber="[)> 06 11V6255595";

//Write to XML
var xmlValue = Convert.ToBase64String(Text.Encoding.UTF8.GetBytes(MatNumber));
xmlNode.Value = xmlValue;

//Reading from XML
var convertedValue = Text.Encoding.UTF8.GetString(Convert.FromBase64String(xmlNode.Value));


I'm just making up the XML node stuff here as I don't know what library you're using to read/write the XML. If you're using some sort of serializer then it should already be encoding the string for you most likely.

1 Vote 1 ·

Yes I use XDocument.

0 Votes 0 ·
Show more comments
Bruce-SqlWork avatar image
1 Vote"
Bruce-SqlWork answered Bruce-SqlWork edited

it pretty simple. you encode special characters as:

 &#<ascii decimal code>;  

for example "<" is

&#60 ;




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

You can also define a convenient format for your needs. For example, you can write the Group Separator as “[GS]” text, etc.:

 <EANCode>[RS]06[GS]11V6255595[GS]P605555501[GS]16SV[GS]Q2330[GS][RS][EOF]</EANCode>

This assumes that “[ ]” does not appear in other places. A simple string.Replace can be used for conversions.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.