开发者

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

开发者 https://www.devze.com 2023-03-25 20:34 出处:网络
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

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.


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.

  • 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.


Having a big file embedded into your C# code as a string literal is a really bad idea. It's a particularly bad idea to have separate lines concatenated with the + operator because strings are immutable and you'd be creating a zillion string objects which are instantly thrown away Chris points out that the compiler will do the literal concatenation at compile time.

You could just read in the XML line as a string with a single line:

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

If you're trying to embed the data file in your project's exe then you can do that with the project's Resources.


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

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

The same can be done to get an array of strings...

string[] xmlContent = System.IO.File.ReadAllLines(pathToXML);


string fileContent = File.ReadAllText(pathToFile))
                         .Replace(Environment.NewLine, "+");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号