How to write an XML tag with double quotes using c

2019-01-28 16:47发布

问题:

Good afternoon,
I would like to add the simpliest tag into a file, using a CMD batchfile, but it seems that the double quotes are spoiling the party:

From other StackOverflow posts I am aware of the double quote state machine and the usage of ^" (^ as an escape character). Still I can't make it work:
Hereby my attempts (with their results):

C:\>echo <tag variable="value">  // very naïve
The syntax of the command is incorrect.

C:\>echo "<tag variable="value">"  // let's use quotes for delimiting the string
"<tag variable="value">"

C:\>echo "<tag variable=^"value^">" // let's use the escape character
The system cannot find the path specified.

C:\>echo "<tag variable=^"value^"> // what if the state machine is not switched back?
The syntax of the command is incorrect.

C:\>echo "<tag variable=^"value"> // desperate people do weird things :-)
"<tag variable=^"value">

I have also done some tests using the escape character in front of the tag characters < and > (as those have their own significance in CMD) but also there no good results.
Also first putting the entry within a variable (set test="...") does not solve the issue.
As mentioned, I am getting desperate. The only thing I want to do is writing this into a text file:

<tag variable="value">

Can anybody help me out here?
Thanks in advance

回答1:

the characters you want to escape are not the " but the <.
Then the correct syntax is this one :

C:\>echo ^<tag variable="value"^>


回答2:

Shouldn't you use the "\" char to escape a double quote ? Like "echo "< tag blabla=\"value\" >" ?

EDIT

Seems like :

echo ^<tag variable=^"value^"^> 

works fine !



回答3:

try with delayed expansion:

setlocal enableDelayedExpansion
set "tag_line=<tag variable="value">"
echo !tag_line!