Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Salem to all;
i was searching about printing and repoting in light switch and i did found this solution :
http://blogs.msdn.com/b/bethmassi/archive/2010/09/10/using-microsoft-word-to-create-reports-for-lightswitch-or-silverlight.aspx[^]

but the problem is the solution is in vb and i did not convert it to c# , this is the code that i want to convert it:

XML
Imports System.Runtime.InteropServices.Automation
Imports <xmlns:ns0="urn:microsoft:ordermanager:customer">

Namespace LightSwitchApplication
    Module MyReports

        Public Sub RunCustomerReportFixedTemplate(ByVal cust As Customer)
            If AutomationFactory.IsAvailable Then
                Try
                    'Create the XML data from our entity properties dynamically
                    Dim myXML = <customer>
                                    <%= From prop In cust.Details.Properties.All
                                        Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
                                    %>
                                </customer>

                    Using word = AutomationFactory.CreateObject("Word.Application")
                        Dim resourceInfo = System.Windows.Application.GetResourceStream(New Uri("CustomerDetails.docx", UriKind.Relative))
                        Dim fileName = CopyStreamToTempFile(resourceInfo.Stream, ".docx")

                        Dim doc = word.Documents.Open(fileName)
                        'Grab the existing bound custom XML in the doc
                        Dim customXMLPart = doc.CustomXMLParts("urn:microsoft:ordermanager:customer")

                        Dim all = customXMLPart.SelectSingleNode("//*")
                        Dim replaceNode = customXMLPart.SelectSingleNode("/ns0:root[1]/customer[1]")

                        'replace the <customer> node in the existing custom XML with this new data
                        all.ReplaceChildSubtree(myXML.ToString, replaceNode)

                        word.Visible = True

                    End Using
                Catch ex As Exception
                    Throw New InvalidOperationException("Failed to create customer report.", ex)
                End Try
            End If
        End Sub

        Public Sub RunCustomerReportDynamicTemplate(ByVal cust As Customer)
            If AutomationFactory.IsAvailable Then

                Try
                    Dim templateFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                                       "\Reports\CustomerDetails.docx"

                    'Create the XML data from our entity properties dynamically
                    Dim myXML = <ns0:root>
                                    <customer>
                                        <%= From prop In cust.Details.Properties.All
                                            Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
                                        %>
                                    </customer>
                                </ns0:root>

                    Using word = AutomationFactory.CreateObject("Word.Application")

                        Dim tempFile = GetTempFileName(".docx")
                        File.Copy(templateFile, tempFile)

                        Dim doc = word.Documents.Open(tempFile)

                        'Add the new custom XML part to the document
                        Dim customXMLPart = doc.CustomXMLParts.Add(myXML.ToString())

                        'bind any content controls that we find based on the title of the control
                        For i = 1 To doc.ContentControls.Count
                            Dim ctrl = doc.ContentControls(i)

                            If Not ctrl.XMLMapping.IsMapped Then

                                ctrl.XMLMapping.SetMapping("/ns0:root[1]/customer[1]/" + ctrl.Title.ToString.ToLower(),
                                            "xmlns:ns0=""urn:microsoft:ordermanager:customer""", customXMLPart)
                            End If
                        Next

                        word.Visible = True
                    End Using
                Catch ex As Exception
                    Throw New InvalidOperationException("Failed to create customer report.", ex)
                End Try
            End If
        End Sub

        Private Function CopyStreamToTempFile(ByVal stream As System.IO.Stream, ByVal ext As String) As String
            Dim path = GetTempFileName(ext)
            'Create the temp file
            Dim file = System.IO.File.Create(path)
            file.Close()
            'Write the stream to disk
            Using fileStream = System.IO.File.Open(path,
                                                   System.IO.FileMode.OpenOrCreate,
                                                   System.IO.FileAccess.Write,
                                                   System.IO.FileShare.None)

                Dim buffer(0 To stream.Length - 1) As Byte
                stream.Read(buffer, 0, stream.Length)
                fileStream.Write(buffer, 0, buffer.Length)

                fileStream.Close()
            End Using
            Return path
        End Function

        Private Function GetTempFileName(ByVal ext As String) As String
            'Return a uinuqe file name in My Documents\Reports based on a guid
            Dim path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Reports"
            If Not Directory.Exists(path) Then
                Directory.CreateDirectory(path)
            End If

            Dim filename = Guid.NewGuid().ToString() & ext
            path = System.IO.Path.Combine(path, filename)

            Return path
        End Function
    End Module

End Namespace





all this class i did not convert it properly and no online converter did convert properly too,
specially this part :

XML
Dim myXML = <customer>
                                   <%= From prop In cust.Details.Properties.All
                                       Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
                                   %>
                               </customer>


any help for this???
Posted

No need to ask such questions every time you need to translate between those languages. You can always do it automatically.
Please see my past answers:
COde Line Interpretatio of C# to VB.NET[^],
Need to convert vb code to c#[^],
FixedPage to ContentPage convert c# code into vb.net[^],
Source Code from a exe[^].

Pay special attention for most robust off-line method using open-source ILSpy. It provides great quality of translation.

—SA
 
Share this answer
 
Here you go:

Click me![^]
 
Share this answer
 
Comments
The-vister 25-Sep-13 11:27am    
I did Try this web page for converting but no result, try it if you want and see the result
Richard C Bishop 25-Sep-13 11:32am    
Does the code as you have it now compile as VB?
Thomas Daniels 25-Sep-13 12:10pm    
Probably the reason why the converter can't convert it because it can't convert the XML code. In VB.NET, it's valid to put XML code in a variable like this:

<small>
Dim myXML = <ns0:root>
<customer>
<%= From prop In cust.Details.Properties.All
Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %>
%>
</customer>
</ns0:root>
</small>

However, that's not valid in C#, and probably the converters can't convert that.
Richard C Bishop 25-Sep-13 12:13pm    
You are correct. That is the conclusion I came to and was trying to get the OP to think about what they were attempting to do.
The-vister 26-Sep-13 1:23am    
this code in vb is working , and no error after compiling the code.
 
Share this answer
 
Please see

Code Converter
[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900