Using C# how can i format a given xml file into a single single line (without spaces)?
My output is giving symbols if there are spaces and new lines.
Use this:
public static string StripXmlWhitespace(string Xml)
{
Regex Parser = new Regex(#">\s*<");
Xml = Parser.Replace(Xml, "><");
return Xml.Trim();
}
You can use string's Replace method to format xmlString and then save it to output:
string singleLineXml = xml.Replace(System.Environment.NewLine, " ")
or
string singleLineXml = xml.Replace("\r\n", " ")
After removing line breaks > remove spaces:
singleLineXml.Remove(' ');
Yes #Steve Wellens, Remove(' ') is a bad idea.. let's try
singleLineXml.Replace("> <","><");
And i found relative thread, may be it helps Writing string to XML file without formatting (C#)
Related
I'm trying to remove new lines from a text file. Opening the text file in notepad doesn't reveal the line breaks I'm trying to remove (it looks like one big wall of text), however when I open the file in sublime, I can see them.
In sublime, I can remove the pattern '\n\n' and then the pattern '\n(?!AAD)' no problem. However, when I run the following code, the resulting text file is unchanged:
public void Format(string fileloc)
{
string str = File.ReadAllText(fileloc);
File.WriteAllText(fileloc + "formatted", Regex.Replace(Regex.Replace(str, "\n\n", ""), "\n(?!AAD)", ""));
}
What am I doing wrong?
If you do not want to spend hours trying to re-adjust the code for various types of linebreaks, here is a generic solution:
string str = File.ReadAllText(fileloc);
File.WriteAllText(fileloc + "formatted",
Regex.Replace(Regex.Replace(str, "(?:\r?\n|\r){2}", ""), "(?:\r?\n|\r)(?!AAD)", "")
);
Details:
A linebreak can be matched with (?:\r?\n|\r): an optional CR followed with a single obligatory LF. To match 2 consecutive linebreaks, a limiting quantifier can be appended - (?:\r?\n|\r){2}.
An empirical solution. Opening your sample file in binary mode revealed that it contains 0x0D characters, which are carriage returns \r. So I came up with this (multiple lines for easier debugging):
public void Format(string fileloc)
{
var str = File.ReadAllText(fileloc);
var firstround = Regex.Replace(str, #"\r\r", "");
var secondround = Regex.Replace(firstround, #"\r(?!AAD)", "");
File.WriteAllText(fileloc + "formatted", secondround);
}
Is this possibly a windows/linux mismatch? Try replacing '\r\n' instead.
actually i have text file with columns and rows colsep is "'" and row sep is "/"
and my text file is sfsf'fsfsdf'sdfsdf/dfsdf'sfsf'sfsfs/
if my text file contains "/" at the end of the file how to check and remove it it is there
You can use string.TrimEnd()
string str = File.ReadAllText(path);
str = str.TrimEnd('/');
You can use following code:
string fileString = = File.ReadAllText(your file path);
if(fileString[fileString.Length-1].Equals('/'))
{
//your code here;
}
OR you can use:
if(fileString.EndsWith('/'))
{
//yourcode
}
We can check individual characters in string using their index as we all know that:
string is the sequence of characters
this will solve your problem to check last character in file.
i want to read from a text file in C#. But I want all the lines in the file to be concatenated into one line.
for example if i have in the file as
ABCD
EFGH
I need to read ABCDEFGH as one line.
I can do this by reading one line at a time from the file and concatenating that line to a string in a loop. But are there any faster method to do this?
string.Join(" ", File.ReadAllLines("path"));
Replace " " with "" or any other alternative "line-separator"
Example file:
some line
some other line
and yet another one
With " " as separator:
some line some other line and yet another one
With "" as separator:
some linesome other lineand yet another one
Use this:
using (System.IO.StreamReader myFile = new System.IO.StreamReader("test.txt")) {
string myString = myFile.ReadToEnd().Replace(Environment.NewLine, "");
}
What is a one line for you?
If you want to put the entire content of a file into a string, you could do
string fileContent = File.ReadAllText(#"c:\sometext.txt");
If you want your string without newline characters you could do
fileContent = fileContent.Replace(Environment.NewLine, " ");
string file = File.ReadAllText("text.txt").Replace("\r\n", " ");
hoping you can help
I have the following code
List<string> comconfig = populate.resolveconfig(_varibledic, populate.GetVaribles[0].Substring(populate.GetVaribles[0].IndexOf("=") + 1)); //get the aray of strings
string config = ""; //create a empty otput string
config = #"\rtf1\ansi\deff0\deftab240 {\fonttbl {\f000 Monaco;} {\f001 Monaco;} } {\colortbl \red255\green255\blue255; \red000\green000\blue000; \red255\green255\blue255; \red000\green000\blue000; }";
config = config + #"\f96\fs20\cb3\cf2 \highlight1\cf0 "; // assigned rtf header to output string
foreach (var strings in comconfig) //loop though array adding to output string
{
config = config + strings + #"\par ";
}
config = config + "}"; //close of RTF code
So trying to create a RTF string that I can later display. comconfig is an array of strings with some RTF mark up for highlighting and stuff.
trouble is that if I use # then I get double \ which mess up the RTF, and if i dont use them, then the escape charatures mess up the code??
what is the best way to build up this string by adding a preformated RTF header and the aray of strings in the middle. it is displayed finaly in a RTF.textbox. or converted to a plain text string at the users request. I need to ignore the escape charatures with out messing up the RTF?
Cheers
Aaron
No, you don't get a double \. You're getting confuzzled by the debugger display of the string. It shows you what the string looks like if you had written it in C# without the #. Click the spy glass icon at the far right and select the Text Visualizer.
Just wondering if there is an easy way around my issue. If I want to place a large chunk of HTML into a string, how's it possible without escaping the HTML first? There is so much HTML which is used for my MySpace bot (inserting into profiles) that it will take forever to escape.
Following what #MattMitchell suggested, you can include the file as a resource in your project. Then you only have to reference it (MyNameSpace.Properties.Resources.MyHTMLFile) to get the contents as a string.
It isn't. But you can use a verbatim string literal (prefixed with #) to make your life slightly easier - you'd only have to replace " with "" to escape the string.
Put it in a file, load the file.
Example:
string myHTML;
using (FileStream file = new FileStream("path.txt", FileMode, FileAccess))
{
using (StreamReader sr = new StreamReader(file))
{
myHTML = sr.ReadToEnd();
sr.Close();
}
file.Close();
}
If you want to insert variables into your HTML, replace the variable locations with {0} .. {n} and then use string.Format();
E.g.
<html><div>{0} = {1}</div></html>
and in your C#
string myHTMLwithVars = string.Format(myHTML, var1, var2);
Alternatively (and more manageable if there are lots of variables or the order is likely to change), name each "variable spot" in your HTML and use string replace.
<html><div>{username} = {randomImageFile}</div></html>
c# change:
string myHTMLwithVars = myHTML
.Replace("{username}", usernameVar)
.Replace("{randomImageFile}", "image.jpg");
Why not read this HTML from a file, or an embedded resource in your project? That way no escaping is necessary.
to read from file:
string contents = File.ReadAllText("path/to/file.htm");