codehutch.com

Online Code Repository

Here is a sample source code on how to read and load xml file via LINQ, extract the data [using a query] and export it to a CSV file.

The XML file (employee.xml)

<xml>
<Employee id=”1″>
<Name>John Doe</Name>
<Position>President</Position>
<Salary>$500,000</Salary>
</Employee>
<Employee id=”2″>
<Name>Jane Doe</Name>
<Position>Vice President</Position>
<Salary>$250,000</Salary>
</Employee>
<Employee id=”3″>
<Name>Jen Doe</Name>
<Position>Manager</Position>
<Salary>$50,000</Salary>
</Employee>
</xml>


The code

‘Libraries
Imports System
Imports System.IO
Imports System.Text
Imports System.Xml.Linq
Imports Microsoft.VisualBasic.FileIO

‘=====================================

‘Read/Load xml file
Dim employees As XElement = XElement.Load(”employee.xml”)
Dim outputData As New StringBuilder

‘query on loaded xml data and output as String Enumeration
Dim xmlData = _
From emp In employees.<Employee> _
Select String.Format(”"”{0}”",”"{1}”",”"{2}”",”"{3}”"”, _
emp.@id, _
emp.<Name>.Value, _
emp.<Position>.Value, _
emp.<Salary>.Value)

‘Traverse enum and append extracted data
For Each row In xmlData
outputData.AppendLine(row)
Next

‘Output as .txt file
File.WriteAllText(”ParsedData.csv”, outputData.ToString)

The output as CSV file (ParsedData.csv)

“1″,”John Doe”,”President”,”$500,000″
“2″,”Jane Doe”,”Vice President”,”$250,000″
“3″,”Jen Doe”,”Manager”,”$50,000″



Tags:

I’m currently using Telerik Treeview and I’m fascinated on how great its features are.  I have an example on NodeClick event.

Highlight parent/s when a node is clicked

The Treeview

<telerik:RadTreeView ID=”MyTreeview” MultipleSelect=”true” OnNodeClick=”MyTreeviewClick” />

Note: MultipleSelect must be set to True

The Click routine

Protected Sub MyTreeviewClick(sender As Object, e As RadTreeNodeEventArgs)
SelectParent(e.Node)
End Sub

Highlight parent function

Protected Sub SelectParent(node As RadTreeNode)
If node.ParentNode IsNot Nothing Then
node.ParentNode.Selected = True
End If
End Sub

If you want to highlight all parents of selected node, you can always call it recursively

Protected Sub SelectParent(node As RadTreeNode)
If node.ParentNode IsNot Nothing Then
node.ParentNode.Selected = True
SelectParents(node.ParentNode)
End If
End Sub

Calling a routine on selected node

You can use a variety of Nodes properties and functions. In this example I use node’s level property which returns the level [integer] of the the current node. This is helpful if you have static node levels. I’m calling my function ONLY on specific node level.

Protected Sub MyTreeviewClick(sender As Object, e As RadTreeNodeEventArgs)
If e.Node.Level = 2 Then
//Do something here when any of the 2nd level node is clicked
End If
End Sub

You can also use this to highlight parents only on selected node levels.

Protected Sub MyTreeviewClick(sender As Object, e As RadTreeNodeEventArgs)
If e.Node.Level = 2 Then
SelectParent(e.Node)
End If
End Sub

Hope this helps :)



Tags:

To assign a role to a new user, we usually use the following code. We call this on the CreatedUser event of the ASP.NET CreateUserWizard Control:

protected void CreatedUser(object sender, EventArgs e) {
Roles.AddUserToRole(cuwRegister.UserName, “Registered User”);
}

The code worked fine for me but not when I customized the CreateUserWizard Control where I encountered this error:

The parameter ‘username’ must not be empty.
Parameter name: username

For some reason, the cuwRegister.UserName gives an empty string thus the error. So I used the FindControl method to access the UserName textbox:

TextBox userName = (TextBox)cuwRegister.CreateUserStep.ContentTemplateContainer.FindControl(”UserName”);
Roles.AddUserToRole(userName.Text, “Registered User”);

To learn how to customize the CreateUserWizard Control, go to MSDN.



Tags:

Some tasks are just too complicated for dynamic query building and/or require a greater level of control. To handle this, SubSonic supports stored procedures. Each stored procedure will produce an equivalent static method in the class defined in the configuration file. By default this is SPs. Each method will have one parameter for each stored procedure parameter and return a StoredProcedure object.

SubSonic.StoredProcedure sp = SPs.CustOrderHist(customerID);

If you have created new stored procedures, read my post on how to:
Add New Stored Procedures to dashCommerce

Now that SubSonic recognizes your custom stored procedures, the next question is how to access/get their return value?

Read the rest of this entry »



Tags:

All stored procedures included in the configuration file (Store/App.config) have an equivalent static method in: Store/Models/Generated/StoredProcedures.cs

When I was new to dashCommerce I didn’t figure this out right away.

If you created new stored procedures, you need to update this SubSonic-generated StoredProcedure class to include your new stored procedures. To do so, modify the configuration file, look for the “includeProcedureList” attribute. Add the names of your new stored procedures in the list. Notice this line:

stripSPText=”dashCommerce_Store_”

Read the rest of this entry »



Tags:

« Previous Entries