简体   繁体   English

如何使用LINQ to XML将“输出到XML文件

[英]How to use LINQ to XML to output " to an XML file

I am doing some XML parsing, and I want to be able to have an element that looks like the following: 我正在做一些XML解析,我希望能够有一个类似于以下内容的元素:

<PostBuildEvent>
   copy &quot;$(BPL_OUTPUT)\$(OUTPUTFILENAME)&quot; &quot;$(QUICKSET_INSTALL)&quot;  
 </PostBuildEvent>

I am using the .NET XDocument class, calling the XDocument.Save() method. 我正在使用.NET XDocument类,调用XDocument.Save()方法。

The value of this PostBuildEvent tag is stored in a constant variable, like the following: 此PostBuildEvent标记的值存储在一个常量变量中,如下所示:

public const string PostBuildEventScript = 
  "copy &quot;$(BPL_OUTPUT)\\$(OUTPUTFILENAME)&quot; &quot;$(QUICKSET_INSTALL)&quot;";

I want to copy the value of this variable to this XML element, but what is printed to the XML document is: 我想将此变量的值复制到此XML元素,但是打印到XML文档的内容是:

<PostBuildEvent>
copy &amp;quot;$(BPL_OUTPUT)\$(OUTPUTFILENAME)&amp;quot; &amp;quot;$(QUICKSET_INSTALL)&amp;quot;
</PostBuildEvent>

As you can see, there's an extra amp; 如您所见,有一个额外的放大器。 that I don't want. 我不想要的 I also tried hard coding the variable like: 我还尝试对变量进行硬编码,例如:

public const string PostBuildEventScript = 
         "copy \"$(BPL_OUTPUT)\\$(OUTPUTFILENAME)\" \"$(QUICKSET_INSTALL)\"";

But this resulted in: 但这导致:

<PostBuildEvent>
     copy "$(BPL_OUTPUT)\$(OUTPUTFILENAME)" "$(QUICKSET_INSTALL)"
</PostBuildEvent>

But as you can see, those are actual double quotes and I wanted "&quot". 但是如您所见,这些是实际的双引号,我想使用“”。

How do I handle this problem? 我该如何处理这个问题?

You should escape & and ; 您应该逃脱&; .

&#38;quot&#59;

You can read it this Article for more escaping of special characters in xml. 您可以阅读此文章,以更多地转义xml中的特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM