C# Replace Resource File Values - c#

I'm not sure how to ask this question, but hopefully someone will understand and please feel free to correct my lack of terminology.
I'm using resource files to display the website in various languages. There's a section in a sentence that is driven by data from a DB, which we have a method for, and grabs it's resource from a local resource file.
Below is what our default.aspx file looks like:
<html>
<body>
<h1>Hello,</h1>
<p><asp:Localize runat="server" Text="<%$ Resources: PersonalAttentionParagraph %>" /></p>
</body>
</html>
The local resource file contains:
...
<data name="PersonalAttentionParagraph" xml:space="preserve">
<value>Blah... 14:1 ...more blah!.</value>
</data>
...
That 14:1 value needs to come from a function:
string GetValue(){
return "14:1";
}
Question: How do you properly create a replace functionality calling a resource file in this manner?
I was thinking of replacing the Resource file value 14:1 with ##VALUE## and then calling a Replace() on it, but I'm not sure if that's the correct way of doing it.
Thank you.

It's fairly common to put "blah... {0} .. more blah" in the resource file. When you use it, just use string.format(yourResourceString,someComputedValue.ToString()).

Related

C# - create and write HTML file with variables

Basiclly I'm trying to create an HTML, I already have it written but I want the user to be able to put some text on the textboxes and saving it into strings and use later when creating the HTML file.
I tried playing abit with StreamWriter but I don't think that will be the best idea.
Also I want it to open on the default web browser , or just on IE if it's easier after the file is created.
I really need help as I'm struggling especially with the creating part.
Thanks for reading!
You can also do this without external libraries.
Set up your HTML file as follows:
<!DOCTYPE html>
<html>
<header>
<title>{MY_TITLE}</title>
</header>
<body></body>
</html>
Then edit and save the HTML from C#:
const string fileName = "Foobar.html";
//Read HTML from file
var content = File.ReadAllText(fileName);
//Replace all values in the HTML
content = content.Replace("{MY_TITLE}", titleTextBox.Text);
//Write new HTML string to file
File.WriteAllText(fileName, content);
//Show it in the default application for handling .html files
Process.Start(fileName);
If you already have the HTML you want to export (just not customized), you could manually add format strings to it (like {0}, {1}, {2}) where you want to substitute text from your app, then embed it as a resource, load it in at runtime, substitute the TextBox text using string.Format, and finally write it out again. This is admittedly a really fragile way to do it, as you need to make sure the number of parameters agrees between the resource file and your call to string.Format. In fact, this is a horrible way to do it. Actually, you should do it the way #EmilePels suggests, which is basically a less fragile version of this answer.

How do I store html page in a xml file?

I have a small application written in c# as a console app that I want to use to send an email. I was planning on storing the email inside an xml file along with other information that the message will need like a subject. However there seems to be a problem because the XML file doesnt like </br> characters.
Im wondering what I should do in order to store a html email do I just have to keeo the body html in a seperate html file and then read each line into a StreamReader object?
The easiest way would be to store the HTML content in a CDATA section:
<mail>
<subject>Test</subject>
<body>
<![CDATA[
<html>
...
</html>
]]>
</body>
</mail>
Use a CDATA section, that will contain your email HTML code :
<?xml version="1.0"?>
<myDocument>
<email>
<![CDATA[
<html>
<head><title>My title</title></head>
<body><p>Hello world</p></body>
</html>
]]>
</email>
</myDocument>
You can use CDATA section in your XML - here you can read about it.
You could store the HTML as CDATA within the XML.
But looking at what you are trying to do, you may wish instead look at the System.Web.UI.WebControls.MailDefinition class, as it already contains a reasonable way of using mail templates.
The msdn documentation gears towards it being used in WinForms apps, but you can simply use a ListDictionary to fill the replacements.
Here is a simplistic example, to give an idea of how MailDefinition can be used, I won't go into to much detail, as it's a little outside of the scope of the original question.
protected MailMessage GetNewUserMailMessage(string email, string username, string password, string loginUrl)
{
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = "~/mailtemplates/newuser.txt";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%username%>", username);
replacements.Add("<%password%>", password);
replacements.Add("<%loginUrl%>", loginUrl);
return mailDefinition.CreateMailMessage(email, replacements, this);
}

i have to write "(Ft³)" on label from resource file. please help me

Actually i have to write "(Ft³)" on label from resource file.
When i write in string as hard code "ft\u00B3" then its convert on label as "Ft³", but i want to access from resource file. so please help.........
thnx
Just paste "Ft³" into the resources designer - not "ft\u00B3"
It gets stored as:
<data name="Ftcubed" xml:space="preserve">
<value>Ft³</value>
</data>
in the .resx file

Intellisense - Javascript in a string

I have a long javascript in a string and programatically using RegisterClientScriptBlock, I add it to my page.
Is there any way to have the intellisense detect my javascript inside the string?
Code:
string Script0 =
#"
function dummy()
{
}
var PTRValues = new Array();
...
...
..
";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myCustomScriptBlock", Script0, true);
No, you can't get intellisense inside the JS string. The IDE doesn't know this particular string is JS.
If it's long don't put it in the *.cs file. Instead store it in a *.js. If you really want you can load the file into memory at runtime and serve it embedded in the html instead of referenced.
Unfortunately, this is not possible.
The best solution is to make put the code separate .js file, then write the following:
Page.ClientScript.RegisterClientScriptBlock(
GetType(),
"myCustomScriptBlock",
File.ReadAllText(myJSFilePath),
true
);
For optimal performance, you should read it only once, then store in in the cache.
Ok, these guys are getting close...
Don't EVER embed scripts in code. Always embed as resource or for prototyping and develepment use ClientScript to render a <script/> tag and reference a .js file.
There are just too many reasons wny you would not want to embed script in code to list. google it.
What you are after is to render some javascript from the codebehind via ClientScript and you would like design time intellisense support?
Ok,
To get intellisense you will need a .js of some kind. The approach I suggest, to promote maintainability and prevent dupe scripts that can get out of sync is:
create an EMPTY file called myScript.js.
create another script containing your code named myScript-vsdoc.js
mark myScript-vsdoc.js as embedded resource and serve it as and embedded web resource
meanwhile, back in the IDE, add a script tag pointing to myScript.js, which is an EMPTY file
press SHIFT-CTRL-J and bingo, you have intellisense for your embedded script, your embedded script is in a source file that is editable and discoverable and you have no duplication.
That is how i do it.

Consuming data from WikiNews

I have been scouring the net but I can't seem to find any examples of consuming data from WikiNews. They have an RSS feed with links to individual stories as HTML, but I would like to get the data in a structured format such as XML etc.
By structured format I mean an XML file for each story that has a defined XML schema (XSD) file. See: [http://www.w3schools.com/schema/schema_intro.asp][2]
Has anyone written a program that consumes stories from WikiNews? Do they have a documented API?
I would like to use C# to collect selected stories and store them in SQL Server 2008.
[2]: By "structured format" I mean something like an XML schema (XSD) file. See: http://www.w3schools.com/schema/schema_intro.asp
The software they use has an API but I'm not sure if WikiNews supports it.
Their feed: http://feeds.feedburner.com/WikinewsLatestNews
If you put that in your browser and read the source, you'll see that it is XML. The XML contains the title, description, a link, etc. Only the description is in HTML.
Here is the beginning of the response:
<rss xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
<title>Wikinews</title>
<description>Wikinews RSS feed</description>
<language>en</language>
<link>http://en.wikinews.org</link>
<copyright>Creative Commons Attribution 2.5 (unless otherwise noted)</copyright>
<generator>Wikinews Fetch</generator>
<ttl>180</ttl>
<docs>http://none</docs>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/WikinewsLatestNews" /><feedburner:info uri="wikinewslatestnews" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site.</feedburner:browserFriendly><item>
<title>Lufthansa pilots begin strike</title>
<link>http://feedproxy.google.com/~r/WikinewsLatestNews/~3/1K2xloPGlmI/Lufthansa_pilots_begin_strike</link>
<description><p><a href="http://en.wikinews.org/w/index.php?title=File:LocationGermany.png&filetimestamp=20060604120306" class="image" title="A map showing the location of Germany"><img alt="A map showing the location of Germany" src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/de/LocationGermany.png/196px-LocationGermany.png" width="196" height="90" /></a></p>
<p><b class="published"><span id="publishDate" class="value-title" title="2010-02-22"></span>Monday, February 22, 2010</b></p>
<p>The pilot's union of <a href="http://en.wikinews.org/wiki/Germany" title="Germany" class="mw-redirect">German</a> airline <a href="http://en.wikipedia.org/wiki/Lufthansa" class="extiw" title="w:Lufthansa">Lufthansa</a> have begun a four-day strike over pay and job security. Operations at subsidiary airlines <a href="http://en.wikipedia.org/wiki/Lufthansa_Cargo" class="extiw" title="w:Lufthansa Cargo">Lufthansa Cargo</a> and <a href="http://en.wikipedia.org/wiki/Germanwings" class="extiw" title="w:Germanwings">Germanwings</a> are also affected by the strike.</p>
<em><a href='http://en.wikinews.org/wiki/Lufthansa_pilots_begin_strike'>More...</a></em><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/WikinewsLatestNews?a=1K2xloPGlmI:9SJI0YV04-M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/WikinewsLatestNews?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/WikinewsLatestNews?a=1K2xloPGlmI:9SJI0YV04-M:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/WikinewsLatestNews?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/WikinewsLatestNews?a=1K2xloPGlmI:9SJI0YV04-M:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/WikinewsLatestNews?d=YwkR-u9nhCs" border="0"></img></a>
</div></description>
<guid isPermaLink="false">http://en.wikinews.org/wiki/Lufthansa_pilots_begin_strike</guid>
<feedburner:origLink>http://en.wikinews.org/wiki/Lufthansa_pilots_begin_strike</feedburner:origLink></item>
Your question is really unclear! but I guess you want to format the feeds of WikiNews, to be readable in a more friendly way (as if you are reading it in WikiNews itself), am I correct?
If so, then you have to know that RSS are XML with a standard format, and not related to WikiNews, and you can transform any RSS feeds to be displayed in -say- HTML with XSLT.
If you need to get the story itself, you can use the given link in the feed, and display it in a webbrowser control (if you are developing a windows application).
Do you need something else other than what I have said?

Categories

Resources