|
Sure, I can translate with no problem
I did it working last night, I was surprised at what the fix was, and was able to generate my xml file.
Would like to see what you came up with, because I think I'm going to use more of this in the future, it's pretty cool.
|
|
|
|
|
Hey there,
Glad it worked out. Post your method here as well, so I can see whether I can improve my solution
ExpandoObject Documentation[^]
JSON Library[^] that I used
dynamic result = new ExpandoObject();
IDictionary<string, object> resultData = (IDictionary<string, object>)result;
resultData.add("templateName", "tshirts");
var templateArray = new [] {
new { Name = "Red", Code = "RED" },
new { Name = "Green", Code = "GRN" },
new { Name = "Blue", Code = "BLU" }
};
resultData.add("templateArray", templateArray);
string jsonString = JsonConvert.SerializeObject(result);
This will output a JSON string like the one you initially mentioned. I think the code is self explanatory. Feel free to reach out for further clarifications.
Hope this helps, cheers
|
|
|
|
|
Oh, that's pretty slick.
I built my json the old fashioned way using string builder.
I just caught the net4.0 comment, using 2.0 on this project.
I was just fine tuning my function to correct the first record error being sort of blank.
<WebMethod()> _
Public Function create_DynamicCart_Template(
ByVal template_Name As String,
ByVal template_Array() As templateElements) As String
Dim dwExitCode As Integer = 2 'An unknown error has occured
Dim json_response As StringBuilder = New StringBuilder
Try
Dim bTemplates As Boolean = DoesFoldersExist("~/App_Data/Products/dynamicItems/templates")
Dim bDisplay As Boolean = DoesFoldersExist("~/App_Data/Products/pageDisplay")
'Create the First Record
Dim new_Template As dynamicItem3.template = New dynamicItem3.template
Dim new_Element1 As dynamicItem3.templateElement_1 = New dynamicItem3.templateElement_1
new_Element1.Name = template_Array(0).Name
new_Element1.suffix = template_Array(0).Code
If (template_Array(0).Type.ToUpper = "SIMPLE") Then
new_Element1.elementType = dynamicItem3.templateElement_1ElementType.Simple
Else
new_Element1.elementType = dynamicItem3.templateElement_1ElementType.Complex
End If
Dim new_Element1_Record() As dynamicItem3.templateElement_1 = {new_Element1}
Dim new_Element1_Record_Count As Integer = 0
'Now Build the rest of the records
For i As Integer = 1 To template_Array.Length - 1
'Build the XML Array from the JSON Array
Array.Resize(new_Element1_Record, new_Element1_Record.Length + 1)
new_Element1_Record(new_Element1_Record.Length - 1) = New dynamicItem3.templateElement_1
new_Element1_Record(new_Element1_Record.Length - 1).Name = template_Array(i).Name
new_Element1_Record(new_Element1_Record.Length - 1).suffix = template_Array(i).Code
If (template_Array(i).Type.ToUpper = "SIMPLE") Then
new_Element1_Record(new_Element1_Record.Length - 1).elementType = dynamicItem3.templateElement_1ElementType.Simple
Else
new_Element1_Record(new_Element1_Record.Length - 1).elementType = dynamicItem3.templateElement_1ElementType.Complex
End If
new_Element1_Record_Count += 1
Next i
'Package the Array into Pure Sweet XML, and write it to the Disk Drive
Dim DataLocation As String = Nothing
DataLocation = HttpContext.Current.Server.MapPath("~/App_Data/Products/dynamicItems/templates/" & template_Name & ".xml")
Dim XmlResponse As New dynamicItem3.template
XmlResponse.Version = "1.0"
XmlResponse.CreationDate = DateTime.Now.ToString("yyyy-MM-dd")
XmlResponse.templateName = template_Name
XmlResponse.element_1 = new_Element1_Record
'Now Lets Write out the file as a MemoryStream
Dim serializer As XmlSerializer
serializer = New XmlSerializer(GetType(dynamicItem3.template))
'Now we have to write out a file for certification
Dim response_serializer As XmlSerializer = Nothing
response_serializer = New XmlSerializer(GetType(dynamicItem3.template))
Dim Writer As StreamWriter = Nothing
Writer = New StreamWriter(DataLocation)
response_serializer.Serialize(Writer, XmlResponse)
Writer.Close()
Writer = Nothing
response_serializer = Nothing
serializer = Nothing
XmlResponse = Nothing
dwExitCode = 0
Catch ex As Exception
'An error occured building the XML - Send 1
dwExitCode = 1
End Try
With json_response
.Append("{")
.Append(" ""template_ExitCode"" : " & dwExitCode.ToString & " ")
.Append("}")
End With
Dim js_response As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer
js_response.Serialize(json_response.ToString)
js_response = Nothing
Return json_response.ToString
End Function
|
|
|
|
|
Woahh.. That's one long solution I think you can simplify that solution by compiling the logic into one class using a dictionary object or a stringbuilder object internally to hold the data.
I will post a sample, if I get time around one of these days.
|
|
|
|
|
I thought it was pretty optimized, considering it building a XML file, and writing it to disk.
|
|
|
|
|
Hey there,
I am sorry about the really late reply. But I've been working with web technologies and XML manipulation much. One thing I've learnt is that XML is great for data transferring but it's slow and heavy.
For instance, if you want to send one piece of data that is huge XML is fine. But if you want to send lots of small pieces of data, the number of tags generated for each small object, adds an unnecessary weight. I hope this gives you the basic idea of what I mean.
I won't have time to write a fully generalized JSON class But it should be really easy to write a logic to generalize your specific class without using XML. Let me know if you still want it.
Basic idea for it would be to implement an interface on all the classes you want to JSONify (I made that word up :P) and pass it on to a special class. The implementation of the interface would return a JSON data string for that specific object. The special class would bind them all to one piece of data.
Hope this helps, Regards
|
|
|
|
|
That's OK on the late reply, it's a discussion, and I got it working using a class with get and set to accept a JSON array of items. So I understand how to pass non-primitives to a web service now.
I find XML to be really fast. I use XML reader and writer, and a class file to code against. So basically each xml element is like an array, that can be resized with Array.resize.
And yes it does help solidify the techniques needed to accomplish the task.
Thanks for collaborating with me on the subject. Guess not that many people are at that level yet.
|
|
|
|
|
hehe It's my pleasure to have a good discussion and help someone out.
XML is not slow for your eye, but it's really slow when it comes to traversing the nodes (i.e. compared to others of course). Anyways, am sure for your purpose it won't effect much. However I'll try to write a sample code that would perform json serialization easily, over the weekend, and share it.
regards
|
|
|
|
|
It's a holiday weekend here, labor day, but I may send another array to a web service next week. I'm reworking my shopping cart and checkout program, implementing better and faster code, while cleaning up.
Have a great weekend!
|
|
|
|
|
I am using Page.Response.Redirect and the following code is not working:
Page.Response.Redirect("test.aspx")
I never get to the requested page.
I have tried
Page.Response.Redirect("test.aspx", false )
and that does not work.
The code that calls the routine is the following:
onKeyPress="return numbersonly(this, event)"
The user has to enter a value in the textbox for the code to work.
Do you think I need to setup the absolute path in this call?
Thus do you think I need another event to trigger the call to the correct method call?
What would you suggest?
|
|
|
|
|
Correct me if I'm wrong on this
Objects like Request, Response, Server are part of the HttpContext, which I think is a link to the web server, and allows the web server to communicate with asp.net
So when you get more advanced with your code, and you no longer bind objects, then you use
httpContext.current.response.redirect("~/default.aspx")
As far as the folder system goes
If I have a folder structure like
/
/images/
/admin/management/login.aspx
/admin/management/logout.aspx
I'm in the root, and want the image folder
redirect(~/images/default.aspx")
I'm in /admin/management/ and I want the logout page
redirect("logout.aspx")
now I want the root default
redirect("~/")
Using Page is for talking to Page Objects, like the header tag, script tags, register a script via the client script manager
Hope that helps
|
|
|
|
|
dcof wrote: The code that calls the routine is the following
Page.Response.Redirect is a server-side function, yet return numbersonly(this, event) appears to be a call to a JavaScript function. That's not going to work. Here is an example of how you can do a server-side redirect:
<%@ Page Language="vb" AutoEventWireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btnTest_Click(sender As Object, e As System.EventArgs)
Page.Response.Redirect("http://www.google.com/")
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Redirect</title>
</head>
<body>
<form id="frmMain" runat="server">
<asp:Button runat="server" ID="btnTest" Text="Redirect" onclick="btnTest_Click" />
</form>
</body>
</html>
If you want to do it with JavaScript, it would be different (you'd use window.location ).
|
|
|
|
|
|
Can you show me how you use
"window,location("/index.aspx");"
in javascript?
how would you wire up this feature to the web form page?
|
|
|
|
|
I didn't mention that to you, I posted that to stein in a post below yours.
You would have to read all the post to get the full story on that. But it includes some samples on how to wire it up. Read the post first,
[^]
[edit]
I did write that to you, still not clear if your posting back, or trying to do that on the client side.
|
|
|
|
|
Even though the post you pointed out to me looks like it would work, I would rather post back to the server. I need to obtain some data from the server.
How ould I post back to the server?
|
|
|
|
|
|
<html>
<body>
<table id="thread-board">
<tr>
<td width="68%">Subject</td>
<td width="12%">Username</td>
<td width="20%">Date Posted</td>
</tr>
<%=ShowMessages(0)%>
</table>
</body>
</html>
'/////////////////////////////////////////
'///// THREAD - MESSAGES //////////////////
intLevel = 0
Function ShowMessages(intParent)
intLevel = intLevel + 1
strSQL = "SELECT * FROM fmsg WHERE fmsg.msg_parent = " & intParent & " ORDER BY CDATE(msg_date) DESC "
Set rstForumMessages = conn.Execute(strSQL)
Do Until rstForumMessages.EOF
response.write("<tr>")
response.write("<td width='68%'><img src='dot.gif' width='" & ((intLevel * 24)-24) & "'>" & rstForumMessages("msg_object") & "</td>")
response.write("<td>" & rstForumMessages("msg_uid") & "<td>")
response.write("<td>" & rstForumMessages("msg_date") & "<td>")
response.write("</tr>")
ShowMessages(rstForumMessages("msg_id"))
rstForumMessages.MoveNext
intLevel = intLevel - 1
Loop
End Function
'///// THREAD - MESSAGES //////////////////
'////////////////////////////////////////
The function above is an asp classic function that I used to display messages and replies in a forum (a sort of treeview). I tried to translate it in asp net but it doesn't work properly.
Imports System
Imports System.IO
Imports system.web.ui
Imports system.web.ui.webcontrols
Imports system.web.ui.htmlcontrols
Imports system.configuration
Imports system.data
Imports system.data.oledb
Public Class Messages
Inherits page
'////////////////////////////////
Dim CN As OleDb.OleDbConnection
Dim CMD As OleDb.OleDbCommand
Dim SQL As String
Dim ODR As OleDb.OleDbDataReader
Dim ODR2 As OleDb.OleDbDataReader
'/////////////////////////////////////
Function ShowMessages(intParent As Integer) As String
Dim tt2 As String = String.Empty
SQL = " SELECT * FROM fmsg WHERE msg_parent=" & intParent
CMD = New OleDbCommand(SQL, CN)
CN.Open()
ODR2 = CMD.ExecuteReader()
If ODR2.HasRows Then
tt2 = ""
While ODR2.Read
intLevel = intLevel + 1
tt2 += "<table>"
tt2 += "<tr>"
tt2 += "<td rowspan=""2""><img src='dot.gif' style='width:" & ((intLevel * 14) - 14) & "px' /></td>"
tt2 += "<td class='child-title'>" & ODR2("msg_id") & " - " & ODR2("msg_object") & "</td>"
tt2 += "</tr>"
tt2 += "<tr>"
tt2 += "<td class='child-body'>" & ODR2("msg_body") & "</td>"
tt2 += "</tr>"
tt2 += "</table>"
tt2 += getChildNode(ODR2("msg_id"))
intLevel = intLevel - 1
End While
tt2 += ""
End If
CN.Close()
Return tt2
End Function
End Class
modified 9-Aug-12 14:58pm.
|
|
|
|
|
Help with what? Not many people are going to read through this unformatted mess. You did not say what you are trying to do nor did you say anything about any problem.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Sorry for the messy code. I forgot to use preformatted text for the code.
|
|
|
|
|
Which ones which, they both look like classic asp to me,
I must admit that Wes is right on your post and code looking like a mess, That's one messy hay stack you have there.
Clean it up for help
|
|
|
|
|
Sorry for the messy code. I forgot to use preformatted text for the code.
|
|
|
|
|
This is more along the lines of asp.net, in pure form. I don't know what the outside of the haystack looks like, so I have no clue where to inject the html
The use of response.write was popular in classic asp and echo in php, but I think it's poor programming practice. I don't know what the alternative is, I left asp back in 2003, and php in 2005.
Private Sub ShowMessages(ByVal iParent As Integer, ByVal pContainer As Panel)
Dim message(3) As String
Dim myConnectionString As String
myConnectionString = ice5commerce.common.iCE5IniFile.GetSQLConnString()
Dim mySelectQuery As String = "SELECT * FROM fmsg WHERE msg_parent=@Parent"
Dim myConnection As New SqlConnection(myConnectionString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
Dim paramParent As SqlParameter
paramParent = New SqlParameter("@Parent", SqlDbType.Int)
paramParent.Value = iParent
myCommand.Parameters.Add(paramParent)
Try
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
While myReader.Read()
message(0) = myReader.GetBoolean(0).ToString 'mID
message(1) = myReader.GetString(1).ToString 'mObject
message(2) = myReader.GetString(2).ToString 'mBody
End While
myReader.Close()
myConnection.Close()
myReader = Nothing
Catch ex As Exception
End Try
myCommand = Nothing
myConnection = Nothing
If (message.Length > 0) Then
Dim table_Container As Table
table_Container = New Table
With table_Container
.CellPadding = 0
.CellSpacing = 0
.Style.Add(HtmlTextWriterStyle.Width, "100%")
.Attributes.Add("border", "0")
End With
pContainer.Controls.Add(table_Container)
Dim tr_TitleRow As TableRow
tr_TitleRow = New TableRow
table_Container.Controls.Add(tr_TitleRow)
Dim td_TitleRow_Left As TableCell
td_TitleRow_Left = New TableCell
With td_TitleRow_Left
.RowSpan = 2
.Style.Add(HtmlTextWriterStyle.Width, "25%")
End With
tr_TitleRow.Controls.Add(td_TitleRow_Left)
Dim img_UserLevel As UI.WebControls.Image
img_UserLevel = New UI.WebControls.Image
With img_UserLevel
.ImageUrl = "~/image/dot.gif"
End With
td_TitleRow_Left.Controls.Add(img_UserLevel)
Dim td_TitleRow_Right As TableCell
td_TitleRow_Right = New TableCell
With td_TitleRow_Right
.Style.Add(HtmlTextWriterStyle.Width, "25%")
End With
tr_TitleRow.Controls.Add(td_TitleRow_Right)
Dim tr_MessageBody As TableRow
tr_MessageBody = New TableRow
table_Container.Controls.Add(tr_MessageBody)
Dim td_MessageBody As TableCell
td_MessageBody = New TableCell
With td_MessageBody
.CssClass = "child-body"
.Text = message(3)
End With
tr_MessageBody.Controls.Add(td_MessageBody)
Else
'No record was found
End If
End Sub
|
|
|
|
|
Hi,
I am using a gridview (asp.net). in which I have a formatting done to the cells with border color = Black.
{
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color: Black";
}
}.
The same is working fine locally, but when deployed the Black lines becomes bold and thick.
Any one Please help on this. Awaiting response !!!!
|
|
|
|
|
Does it happen across all browsers? What happens if you explicitly set the border-width to "thin"?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|