How to get server side variable value in xsl template? - asp.net

I want use localized strings from resources in xsl template as in aspx page, like this:
<%=GetLocalizedString("grid_numberof_claim")%>. I am trying use
<xsl:text disable-output-escaping="yes">
<![CDATA[<%=GetLocalizedString("grid_numberof_claim")%>]]>
</xsl:text>
but it is not useful.
Actually i can pass localized strings inside XML node, for example "localization". But i am looking for way to get its value in aspx style.

Using ASPX style isn't possible.
You can use XsltArgumentList to send parameters to your XSLT template, as explained here: HOW TO: Execute Parameterized XSL Transformations in .NET Applications
EDIT: Yes, you can pass arguments client-side too.
xmldoc = ... // your xml document
var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");
xslDoc.async = false;
xslDoc.load("YourTemplate.xsl");
xslt.stylesheet = xslDoc;
xslProc = xslt.createProcessor();
xslProc.input = xmldoc;
xslProc.addParameter("param1", 123);
xslProc.addParameter("param2", "abc");
xslProc.transform();
But client-side leads to another solution: You can rename your XSLT file to ASPX and to use <%= %> syntax

Related

How can i extract XML and use it as a datasource?

I am using asp.net VB and I have an XML file containing a set of data, I would like to use it in something like a datalist and where usually you would use a database i would like to use the XML file to produce the information.
Does anyone know how to do this, i have read about transform files but surely i will format the information in the control?
The file has multiple records so in some cases i would need to perform queries on the information through the datasource.
I would maybe look into XML serialization and de-serialization. Using de-serialization you could read your XML into a List(T) object containing a list of your own class objects and use that as a data source for your application.
Heres a link that you may find useful:
http://msdn.microsoft.com/en-us/library/ms731073.aspx
Hope this helps.
Dim ds As New DataSet()
ds.ReadXml(MapPath("data.xml"))
First you have to parse the XML and store that into custom C# object or you can directly pass the XML to your stored procedure and do the codding there for saving it into DB.
Passing the xml to stored procedure and manipulating it there is bit difficult so what I suggest is to parse it in C# and then get a custom object. Once you get it you can do whatever you want to.
Below is the sample code that parse a XML file and generate a custom C# object from it.
public CatSubCatList GenerateCategoryListFromProductFeedXML()
{
string path = System.Web.HttpContext.Current.Server.MapPath(_xmlFilePath);
XDocument xDoc = XDocument.Load(path);
XElement xElement = XElement.Parse(xDoc.ToString());
List<Category> lstCategory = xElement.Elements("Product").Select(d => new Category
{
Code = Convert.ToString(d.Element("CategoryCode").Value),
CategoryPath = d.Element("CategoryPath").Value,
Name = GetCateOrSubCategory(d.Element("CategoryPath").Value, 0), // Category
SubCategoryName = GetCateOrSubCategory(d.Element("CategoryPath").Value, 1) // Sub Category
}).GroupBy(x => new { x.Code, x.SubCategoryName }).Select(x => x.First()).ToList();
CatSubCatList catSubCatList = GetFinalCategoryListFromXML(lstCategory);
return catSubCatList;
}

Passing request file into flex application with flashvar

I am trying to get a xml file with data into Flex application. There are a lot of examples online passing parameter into flex I found very helpful. However, it doesn't really work in my case.
here is my code in HTML:
var flashvars = {};
flashvars.storageStatsXML = "stats.xml";
var params = {};
swfobject.embedSWF("mySWF.swf", "mySWF", "1000", "500", "10.0.0", "js/expressInstall.swf", flashvars, params);
here is the code in mxml:
[Bindable]
public var storageStats:XML;
protected function start(event:FlexEvent):void
{
storageStats = Application.application.parameters.storageStatsXML;
}
And then the XML file got parsed in the application.
I think there is something not right about the code, any thoughts?
Thanks.
The Application.application.parameters.storageStatsXML property is not the XML data you are expecting, it is a String containing the text "stats.xml".
In the same way that the file path "c:\temp\info.txt" (or "/temp/info.txt") isn't the file itself, it just tells you how to find the file on disk.
You will need to use a URLRequest to load the XML file specified by the storageStatsXML property.
Have a look at the Actionscript documentation and here on StackOverflow for examples on how to load external data.

How to ignore comments when parsing xml in asp.net

Seems like this should be easy, but I'm not finding a simple configuration setting. Basically I have a page that will be parsing xml files that may have some comment tags in them. I'm loading it as an xml doc and looping through a particular node of the document and I'm running into problems because it's counting the comment as a child node. Any way to tell asp.net not to look at comments other than writing my own check for <!-- ?
If you use XmlNode, then that has a NodeType property. Ignore the nodes where that has a value of "Comment".
An XNode has the same property.
Use XmlReaderSettings.IgnoreComments:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}

Read in a html page within asp.net mvc

i am porting over a website from asp.
i have one page that i can't figure out how to migrate it.
The page is dynamic in the sense that it reads in other html pages and sticks the content into the main "container" page. In the middle of the asp page it has sections like below
<%
Dim fso1, f11, ts1, s1
Const ForReading1 = 1
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set ts1 = fso1.OpenTextFile("" & Server.MapPath("newsletters/welcome.html") & "", ForReading)
s1 = ts1.ReadAll
Response.Write s1
ts1.Close
set fso1 = nothing
set f11 = nothing
set ts1 = nothing
set s1 = nothing
%>
Any suggestions in ASP.net MVC for best way to read in other html pages and stick them into a page view.
I assume that these are HTML fragments, not full pages. You could convert them to partial views -- pretty trivial, you just add the correct page directive and rename to .ascx. Then you would use Html.RenderPartial to include the partial in your main view. Another way would be to create your own HtmlHelper extension that works like RenderPartial but simply reads the named file and writes it to the response just like you are currently doing.
Ex1:
<% Html.RenderPartial( "welcome.ascx" ); %>
Ex2:
<% Html.RenderHtml( Server.MapPath( "newletters/welcome.html" ) ); %>
Note that in the first case the view file needs to live in the Views directory. In the second case, you can reference the file from anywhere that the worker process has read access. You'll need to create the second method yourself. Perhaps something similar to:
public static class MyHtmlHelperExtensions
{
public static void RenderHtml( this HtmlHelper helper, string path )
{
var reader = new StreamReader( path );
var contents = reader.ReadToEnd();
helper.ViewContext.HttpContext.Response.Write( contents );
}
}
Please note that you'll have to add error handling.
To read the contents of a text file in .Net, use the File.ReadAllText method.
The exact equivalent of your code snippet would be
<%= File.ReadAllText(Server.MapPath("newsletters/welcome.html")) %>
You should write like this:
string html = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/htm/external/header.htm"));

How to spit out a JavaScript variable (type array) from Custom Control

I want to spit out a JS variable array like so from my custom control:
var myArray = [5410, 8362, 6638, 6758, 7795]
caveat is that I want it to also be unique to this control as this control will spit out more JavaScript that will utilize myArray. So I envision something like:
var [control'sID]myArray = [5410, 8362, 6638, 6758, 7795]
and then somehow the rest of the JavaScript that is spit out can reference this somehow where needed in the JS code. I've got certain methods in the other JavaScript that is being spit out that is using myArray.
You can use ClientID
C# Example:
Response.Write("var " + this.ClientID + "myArray = [5410, 8362, 6638, 6758, 7795];");
VB Example:
Response.Write("var " & Me.ClientID & "myArray = [5410, 8362, 6638, 6758, 7795];")
I think you want to generate a javascript array from your custom control, and you want to access it from your main library,
You can use your control's ClientID property to make your arrays unique.
And you can define a global array to keep the array's names to access them from your rest of the client scripts.
EDIT :
I just look at the RegisterExpandoAttribute method, it's an interesting method, I have never heard that before. In this example it looks like it you can add custom attributes to your control.
So you can use it in your custom control like that :
Just add a custom attribute that has your array items with comma separated. and when accessing the custom attribute, simply split them by comma. I think this will work for your question.
At your custom control :
Page.ClientScript.RegisterExpandoAttribute(this.ClientID
, "myArray", "1,2,3,4");
At client side, get your array like that :
var myArray =
document.getElementById('<%= this.ClientID %>').getAttribute("myArray")).split(',');

Resources