I have a open file dialog that open XML file. The regex expression find every string between > and <, and write every string in new line to the rich text box.
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
string s = sr.ReadToEnd();
richTextBox1.Text = s;
}
string txt = richTextBox1.Text;
var foundWords = Regex.Matches(txt, #"(?<=>)([\w ]+?)(?=<)");
richTextBox1.Text = string.Join("\n", foundWords.Cast<Match>().Select(x => x.Value).ToArray());
}
Then I can change those strings. But how can I import those changed strings back to original XML file on its same place?
You could try to replace these strings inside a file, but once you replace something with a different length, it would be simpler to just write the entire file instead.
It looks like the user is able to modify these strings - that's your challenge there: you will have to keep track of which word was where in the original file to replace them back into the data. Furthermore the user is able to remove or add lines to the textbox, what would your application do in that case?
It would be easier to process the xml file using XDocument and store the XElements that contain the original values. XDocument allows you to replace these values and store the file.
Note that since you're not explicitly closing the StreamReader, the file may still be in use when you try to write it. Simply put the StreamReader in a using block to prevent this.
Related
I am reading a text file's contents into a RichTextBox like this:
string contents = File.ReadAllText("MyFile.txt");
myRichTextBox.Document.Blocks.Clear();
myRichTextBox.AppendText(contents);
I am using the RichTextBox to automatically apply some syntax highlighting of sorts. When I try reading the unformatted text as described here to save it back to the file, things happen:
A newline (\r\n) is added to the back of the file, which I don't want unless the user explicitly adds this newline.
When I load the file again, the newline is not displayed in the RichTextEdit, even if it is present in the file.
How can I change this, so that the RichTextBox displays and returns exactly the contents of the text file?
The newline \r\n (CR/LF) is part of the text formatting in the RichTextBox control. Each paragraph while converting to the text will be appended by the \r\n.
This is means when a user press the ENTER button a new paragraph with \r\n is adding to the RichTextBox control. And when StringFromRichTextBox() method, described in the Microsoft documentation is used to extract the text content from a RichTextBox it will return a string in which all paragraphs are separated by the \r\n.
The explanations regarding the comments above:
A newline (\r\n) is added to the back of the file, which I don't want unless the user explicitly adds this newline.
A newline \r\n is adding to the end of the file only as a part of the each paragraph ending.
NOTE: If it is necessary to save and thereafter to load the saved document the TextRange.Save() and TextRange.Load() methods can be used:
public void SaveRtf(RichTextBox rtb, string file)
{
var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
using (var stream = new StreamWriter(file))
{
range.Load(stream.BaseStream, DataFormats.Rtf);
}
}
public void LoadRtf(RichTextBox rtb, string file)
{
var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
using (var stream = new StreamWriter(file))
{
range.Save(stream.BaseStream, DataFormats.Rtf);
}
}
If to save the whole RuchTextBox content the new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text will be used than any text formatting after restoring will be lost.
Could this work? contents.Replace("\r\n", "\n");
I want to open an existing MS Word document, then write a specific text on it.
The existing document has this content:
First car costs ... $.
I would like to add a specific text right after the word costs.
The final text should be :
First car costs 15000 $.
I want to do this using a simple c# application.
I'm having difficulties to find a way to add text at the desired position.
I used a NuGet package called DocX.
Here is my code:
using Novacode;
using System.Text.RegularExpressions;
...
string fileName = #"C:\Users\DAFPC\Documents\WordDoc1.docx";
var doc = DocX.Load(fileName);
doc.ReplaceText("...", "15000",false, RegexOptions.None, null, null, MatchFormattingOptions.SubsetMatch, true, false);
doc.Save();
Process.Start("WINWORD.EXE", fileName);
The code does not replace "..." with "15000". But if I try to replace the word "Car" with "15000" the code works.
Check edit
doc.ReplaceText(toReplace, replacement);
You should wrap it in a function like the following:
static void Main(string[] args)
{
string filename = "test.docx";
ReplaceInDocx(filename, "...", "15000");
}
static void ReplaceInDocx(string filename, string toReplace, string replacement)
{
var doc = DocX.Load(filename);
doc.ReplaceText(toReplace, replacement);
doc.Save();
}
You might find this useful.
Edit: Ok, I see what you mean. The problem is with the way MS Word works. When you enter "..." it automatically gets converted into "…" (ellipsis). What you should do, then, is to search for that instead. Or better, change the search parameter.
ReplaceInDocx(filename, "…", "car");
I am using stream reader for reading a text file.
This is the contents of the .txt file:
</a> Schools's are a suitable public </a>
When I read that text I got:
<a>Schoolss are a suitable public<a>
As you can see I did't receive the quotation. How can I receive the special character in a stream reader?
I used following code:
using (StreamReader reader = new StreamReader(CommonGetSet.FileName, System.Text.Encoding.ASCII))
{
string text = reader.ReadToEnd();
docKeyword = XDocument.Parse(text);
}
The problem you are having is that you are trying to load a text file with an xml reader, i.e. this part:
XDocument.Load(reader);
If you look at this question: What characters do I need to escape in XML documents?, you will see other characters that will be stripped/need escaping too.
If you inspect the StreamReader in the debugger you will see it shows the correct text, something that the answer by #JinsPeter shows. So you need to read in a text file, the easiest way is to use either File.ReadAllText or File.ReadAllLines depending on whether you want the result as a string or string[] respectively:
string contents = File.ReadAllText(path);
string[] lines = File.ReadAllLines(path);
However, if for some reason you really want to use a StreamReader you can read directly from the stream using ReadToEnd, ReadLine or any other appropriate read method:
using (StreamReader reader = new StreamReader(path))
{
string contents = reader.ReadToEnd();
}
However, note that the StreamReader methods will read from the current position in the stream so you may need to set the position yourself.
For a list of other ways to read in a file in C# see this question: How to read an entire file to a string using C#?.
When I printed the same text inside the StreamReader I got the ' .
So the issue is with writing it to XML or HTML. Try to fix that rather than finding issue in StreamReader.
using (StreamReader inputStream = new StreamReader(filepath, System.Text.Encoding.UTF8))
{
string line = inputStream.ReadToEnd();
Console.WriteLine(line);
}
Please, I want to find a word and replace it with another word in word doccument using openXML
I use this method
public static void AddTextToWord(string filepath, string txtToFind,string ReplaceTxt)
{
WordprocessingDocument wordDoc = WordprocessingDocument.Open(filepath, true);
string docText = null;
StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream());
docText = sr.ReadToEnd();
System.Diagnostics.Debug.WriteLine(docText);
Regex regexText = new Regex(txt);
docText = regexText.Replace(docText,txt2);
StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create));
System.Diagnostics.Debug.WriteLine(docText);
wordDoc.Close();
}
but
docText
return just the head of the page that the xml shema of the document.
<?xml version="1.0" encoding=.......
Check Your Strings
If you want to replace a specific word or phrase within your existing content, you may just want to use the String.Replace() method as opposed to performing a Regex.Replace() which may not work as expected (as it expects a regular expression as opposed to a traditional string). This may not matter if you expect to use regular expressions, but it's worth noting.
Ensure You Are Pulling The Content
Word Documents are obviously not as easy to parse as plain text, so in order to get the actual "content", you may have to use an approach similar to the one mentioned here that targets the Document.Body properties instead of reading using a StreamReader object :
docText = wordDoc.MainDocumentPart.Document.Body.InnerText;
Performing Your Replacement
With that said, you currently appear to be reading the contents of your file and storing it in a string called docText. Since you have that string and know your values to find and replace, just call the Replace() method as seen below :
docText = docText.Replace(txtToFind,ReplaceTxt);
Writing Out Your Content
After performing the replacement, you'll just need to write your updated text out to a stream :
using (var sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
Is it possible to use streamreader to find all the lines that match tags and then edit the content inbetween them?
So far i can read all the content of the html file into a rich text box, but i would prefer for it to find each line that has an h4 tag and load it into textbox1, 2 ,3 etc.
how would i do this?
Could you please give an example, i am new to this.
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlgOpen = new OpenFileDialog())
{
try
{
// Available file extensions
dlgOpen.Filter = "All files(*.*)|*.*";
// Initial directory
dlgOpen.InitialDirectory = "L";
// OpenFileDialog title
dlgOpen.Title = "Open";
// Show OpenFileDialog box
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
// new StreamReader
StreamReader sr = new StreamReader(dlgOpen.FileName, Encoding.Default);
// Get all text from the file
string str = sr.ReadToEnd();
// CloseStreamReader
sr.Close();
// Show text
richTextBox1.Text = str;
}
}
From You should use HtmlAgilityPack. and could you give an example of how to use it with html agility pack then?
Follow How to use HTML Agility pack.
You can find examples as well.Here
Please do not try to use Regular Expressions to find and replcase. Because this great answer RegEx match open tags except XHTML self-contained tags begs to avoid that. I was told about it in comments
However I had used them 100% successfully with 110% limited scope. Limited mean my e.g. I have no other tags between any <td> sometext </td> and I wanted to replace all of them with <td> sometext and some newtext </td> and I had done it successfully. But I was 100% sure what number of tags and what maximum depth I had in my HTML with no Unexpected and Irregular behavior. If you have such limited and well defined HTML then These Regular Expressions might help.