Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take XML file and turn into long string for c# coding

Is there any trick or utility that can take an XML file and turn the file into a long string to be used inside of c# code? I dont' want to manually have to copy and paste every line in the XML file. I want each line in the file to be a string line in my c# code and then have a "+" on the end of the line.

like image 268
user31673 Avatar asked Aug 03 '11 18:08

user31673


People also ask

Can we convert XML to string?

You can convert XML data to any of the string or binary types, and you can convert any of the string or binary types to XML.

How do I read an XML file as a string?

In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.

What is a string in XML?

Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one <resources> element. file location: res/values/filename.xml.


2 Answers

Its not a programming question but here's what you're looking for: http://inedo.com/downloads/smartpaster

like image 140
Mrchief Avatar answered Sep 29 '22 11:09

Mrchief


Use your editor

You could paste the XML into a new file in Visual Studio, and use regular expressions search-and-replace to fix up the document.

  • Escape quotes and other problem-causing characters
  • Append " to the beginning and end of the line, and + to the end of the line.

Then you just have to go back and add a variable name at the start, a semi-colon at the end, and paste it into your source code document.

From there, you can optionally reformat the source code document (to indent it nicely) via Edit -> Advanced -> Format Document.

Another option here is to make use of verbatim string literals, by appending @ before the first quote. Then newlines will be maintained, and you can escape quotation marks with "" instead of \".

Rethink your problem

XML files that are stored in strings are somewhat hard to edit, and defeat part of the point of XML - to be human-editable. To deal with this, you could consider adding the XML to your project

  • You could add it to your project as content, and set the properties on the file to copy it to the build directory. Then use the appropriate XmlDocument load-from-file methods, or simply File.ReadAllText.

The advantage (and potentially disadvantage) here is that you can edit the file after compile. The disadvantage is that you have to deploy the Xml file along with your assembly.

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

The disadvantage (and potentially advantage) is that you can't edit the file after compile. The advantage is that you don't have to deploy the Xml file along with your assembly.

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

like image 28
Merlyn Morgan-Graham Avatar answered Sep 29 '22 10:09

Merlyn Morgan-Graham