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.
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.
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.
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.
Its not a programming question but here's what you're looking for: http://inedo.com/downloads/smartpaster
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.
"
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With