<MyXmlType>
<MyXmlElement>Value</MyXmlElement>
</MyXmlType>
I need a small help here. I was trying to change the innertext value of here. After i change the value and save it with Xmldocument object. When I opened the file, I found that it has got saved in this format.
<MyXmlType>
<MyXmlElement>Value</MyXmlElement>
</MyXmlType>>
Please note this </MyXmlType>> ">>". I dont know what is happening. Please help...
My guess is that you were changing the inner text to something which was one character shorter, and then overwriting the file in place instead of overwriting it with a brand new file. That would mean the extra > was left from the previous version of the file.
If you could show the code you're using to write the file, that would help a lot.
Do you get the same problem if you write to a new file?
Related
I have an ASP WebForms app where I use a word template that contains merge fields, to replace them with data extracted from the database. The app works great, the word document is exported, but when trying to print the document, one of the merge fields, which exists in the header, loses it's value and restores to the initial merge field name. Is this something that has to be fixed from the application's code or is this a word settings issue.
Any help is greatly appreciated.
Thank you!
I have managed to solve this problem using OpenXML Productivity Tool. It turns out that you can't add a merge field in the header, so what I did was to put it inside of a textbox. I forgot to mention that part in the initial description. Thus, the text element was buried deep in open xml. When I managed to find it and log what was inside, I found out that I inserted the MERGEFIELD <> MERGEFORMAT. Every time I tried to insert the value that I wanted in this <>, it got reset when I hit print preview. So what I did, based on a suggestion from someone who had a similar problem, was to delete this textbox and create a clean one where I only entered "Test". It needs to have a string inside so that open xml created the element Text (instrTxt).
In C# I did this:
foreach (var hPart in firstDoc.MainDocumentPart.HeaderParts)
{
foreach (var txt in hPart.Header.Descendants<Text>())
{
if(txt.Text == "Test")
{
txt.Text = "My custom text";
}
}
}
So for each header part (because I can't tell for sure in which one it is..it could even be in multiple ones), get all descendants of type text.
I got a few more than I wanted. I also got a few that contained the page number (since I have it in the header as well). So I added an if to check if it's the text element that I wanted. Once I found it, I added the text I wanted.
So long story short, instead of using a merge field in the header, I just used a text. Perhaps it's not the most efficient way of doing this. Maybe the question still remains, (if I could have inserted a merge field in the header and actually made it work without having word reset the value upon print preview? idk), but this worked for me.
I am trying to find solution to this from last 2 hours, I have searched a lot on this but didn't found any solution(may be I am searching with wrong keywords), but the problem is I want remove file properties which contain special characters. Please check attached image for what I want to say.
I am using ASP.NET FileUpload control and C# as programming language. I want to make sure that any file uploaded does not contain any special characters in its properties.
Please help.
Thanks.
Have you tried looping through Image.PropertyItems?
You can modify the image to remove unwanted details via GetPropertyItem() and SetPropertyItem()
References:
https://msdn.microsoft.com/en-us/library/system.drawing.image.propertyitems(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.image.setpropertyitem(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.image.getpropertyitem.aspx
Sample here in StackOveflow:
Value of image property (C#)
The process I currently use to insert a string into a text file is to read the file and modify it as I write the file out again which seems to be how everyone is doing it.
Since .net has such a diverse library I was wondering if there was a specific command for inserting a string at a specific line in a txt file.
I would imagine it would look something like this:
dim file as file
file.open("filePath")
file.insert(string, location) 'this isn't actually an option, I tried
No, there's nothing specifically to do that in .NET.
You can do it very simply if you're happy to read all the lines of text in:
var lines = File.ReadAllLines("file.txt").ToList();
lines.Insert(location, text);
File.WriteAllLines("file.txt", lines);
It would be more efficient in terms of memory to write code to copy a line at a time, inserting the new line at the right point while you copy, but that would be more work. If you know your file will be small, I'd go for the above.
You could simply read all the text into a string, then use the string insert method, e.g.
File.WriteAllText("file.txt", File.ReadAllText("file.txt").Insert(startIndex, "value"));
So this is just a thought... Say I wanted to insert a line into a file at the first linebreak, but I didn't want to read the entire file into memory. Is there a way to just read the file into memory line by line and then, when the first linebreak is reached, insert a line and close the file reader?
Thank you!
Sorry for the earlier typo in my title/description
you can read one byte at a time and intercept the line break character. Then at that position do a stream.WriteLine(). You'll hve to open file for read+write
Edit: I thought of using the FileStream object, but I'm not sure. Any for now try the suggestion mentioned here: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e5077949-5d23-4d26-a530-6d503745a197
Edit again: check How to both read and write a file in C#
Try the following:
using (StreamWriter w = File.AppendText("test.txt"))
{
w.Write("\r\nTest");
w.Flush();
w.Close();
}
Update:
For inserting the line in existing text file (I assume you're working with text files since you mentioned line break): there isn't a way to do it with file I/O API - you must read the text after the first line break, overwrite the file and add original trailing text.
let me brief there is a file (it might be anything sgml,xml,sgml etc).i will give that file as input and read the file using c#..so the job is now that i know that there are certain tags where in between this data is present...so the point is i have to scan that line by line like pattern matching and extract the data which is present in between tags ,...so please help me getting this ...
I think if file is some how in xml format you can use XPath to traverse/manipulate its elements..
http://support.microsoft.com/kb/308333
http://www.codeproject.com/KB/cpp/myXPath.aspx
Regards.