Take the xml file and turn into a long string for c # coding

Is there any trick or utility that can take the XML file and turn the file into a long string that will be used inside C # code? I do not want to manually copy and paste each line into the XML file. I want each line in the file to be a string string in my C # code, and then have a β€œ+” at the end of the string.

+6
source share
5 answers

This is not a programming issue, but what you are looking for: http://inedo.com/downloads/smartpaster

+6
source

Use your editor

You can insert XML into a new file in Visual Studio and use regular search-and-replace expressions to fix the document.

  • Escape quotes and other problematic characters
  • Add " to the beginning and end of the line and + to the end of the line.

Then you just need to go back and add the variable name at the beginning, a half-point at the end and paste it into the source code document.

From there, you can optionally reformat the source code document (to indent it) using Edit β†’ Advanced β†’ Format Document.

Another option here is to use shorthand literals by adding @ before the first quote. Then newlines will be supported, and you can escape the quotation marks "" instead of \" .

Rethink Your Problem

The XML files that are stored in strings are somewhat difficult to edit and destroy part of the XML point so that they are editable. To handle this, you might consider adding XML to your project.

  • You can add it to your project as content and set the properties in the file to copy it to the assembly directory. Then use the appropriate XmlDocument load-from-file methods or just File.ReadAllText .

The advantage (and potential disadvantage) is that you can edit the file after compilation. The downside is that you need to deploy the Xml file along with your assembly.

  • You can add XML to your project as an embedded resource. Then you can use Assembly.GetManifestResourceStream to get the data from the file.

The disadvantage (and potential advantage) is that you cannot edit the file after compilation. The advantage is that you do not need to deploy the Xml file along with your assembly.

Doing this makes it easier for the developer to edit the file than it would be if Xml were overflowed into a string literal.

+6
source

Having a large file embedded in your C # code as a string literal is really bad. It is a particularly bad idea to have separate lines combined with the + operator , because the lines are immutable, and you will create zillion-string objects that are instantly thrown away . Chris indicates that the compiler will perform alphabetic concatenation at compile time.

You can simply read the XML string as a single-line string:

 string my_string = File.ReadAllText("xmlfile.xml"); 

If you are trying to embed a data file in your exe project, you can do this using project resources.

+3
source

Can't you just send the XML path to File.ReadAllText (path)?

 string xmlContent = System.IO.File.ReadAllText(pathToXML); 

You can do the same to get an array of strings ...

 string[] xmlContent = System.IO.File.ReadAllLines(pathToXML); 
0
source
 string fileContent = File.ReadAllText(pathToFile)) .Replace(Environment.NewLine, "+"); 
0
source

Source: https://habr.com/ru/post/894207/


All Articles