Can't find file - asp.net

I'm working on a custom menu system in asp.net that populates a horizontal menu on the fly based on which menu item is selected from the website's main menu.
This 2nd menu is populated from a custom XML file in the website's root directory.
(See http://loganyoung.wordpress.com/2010/06/03/asp-net-horizontal-submenu-from-xml/ for details).
At the time I'd written that post, it did work, but my development environment has changed and now I'm getting an error saying that the XML file can't be found.
Here's my code:
Imports System.Xml
Partial Class Site
Inherits System.Web.UI.MasterPage
Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
Select Case e.Item.Value.ToString
Case "Team"
Dim doc As New XmlDocument
doc.Load("~/Submenus.xml")
Dim NameNodes As XmlNodeList = doc.SelectNodes("/TeamMenu/item/name")
Dim URLNodes As XmlNodeList = doc.SelectNodes("/TeamMenu/item/url")
If NameNodes.Count = URLNodes.Count Then
For i As Integer = 0 To NameNodes.Count - 1
Dim m As New MenuItem
m.Text = NameNodes.Item(i).FirstChild.InnerText
m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText
Menu2.Items.Add(m)
Next
End If
Case "Investments"
Dim doc As New XmlDocument
doc.Load("~/Submenus.xml")
Dim NameNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/name")
Dim URLNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/url")
If NameNodes.Count = URLNodes.Count Then
For i As Integer = 0 To NameNodes.Count - 1
Dim m As New MenuItem
m.Text = NameNodes.Item(i).FirstChild.InnerText
m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText
Menu2.Items.Add(m)
Next
End If
Case "Social Responsibility"
Dim doc As New XmlDocument
doc.Load("~/Submenus.xml")
Dim NameNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/name")
Dim URLNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/url")
If NameNodes.Count = URLNodes.Count Then
For i As Integer = 0 To NameNodes.Count - 1
Dim m As New MenuItem
m.Text = NameNodes.Item(i).FirstChild.InnerText
m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText
Menu2.Items.Add(m)
Next
End If
End Select
End Sub
End Class
And here's the error I'm getting:
Could not find a part of the path 'c:\windows\system32\inetsrc\~\Submenus.xml'.
Menu2 is just a completely empty <asp:Menu> control directly under the main menu on the page.
Can someone tell me what I'm doing wrong please?
Thanks in advance.

XmlDocument.Load is expecting a file path where you are providing a virtual path. Try changing it to this:
doc.Load(Page.MapPath("~/Submenus.xml"))

If you use doc.Load("~/Submenus.xml") this xml must be in your project. Otherwise you have to use server.mappath.

Related

VB.Net OpenXML Conditional Page Break

I am using the OpenXML library to auto generate Word files. I have a function that takes a group of files and merges them into one document. As I merge a new file into a document, I want each file to start on a new page. But, I don't want to have any blank pages. The code I have mostly works, but an issue comes up is if a file being merged in is a filled page, then a page break is still added, resulting in an empty page being added. I am not sure how to best deal with this, to prevent blank pages from being added. Here is my code:
Public Sub MergeFiles(ByVal filePaths As List(Of String), ByVal fileName As String)
Dim newFile As String = HttpRuntime.AppDomainAppPath & "PDF_Templates\TempFolder\catalog-" & Guid.NewGuid.ToString & ".docx"
File.Copy(fileName, newFile)
Dim counter As Integer = 0
For Each filePath As String In filePaths
Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(newFile, True)
Using wordDoc
Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
Dim altChunkId As String = "altChunkId" & counter
Dim chunk As AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId)
Dim fileStream As FileStream = File.Open(filePath, FileMode.Open)
Using fileStream
chunk.FeedData(fileStream)
End Using
Dim AltChunk As AltChunk = New AltChunk()
AltChunk.Id = altChunkId
' Dont add a page break to the first page.
If counter > 0 Then
Dim last As OpenXmlElement = wordDoc.MainDocumentPart.Document.Body.Elements().LastOrDefault(Function(e) TypeOf e Is Paragraph OrElse TypeOf e Is AltChunk)
last.InsertAfterSelf(New Paragraph(New Run(New Break() With {
.Type = BreakValues.Page
})))
End If
mainPart.Document.Body.InsertAfter(Of AltChunk)(AltChunk, mainPart.Document.Body.Elements(Of Paragraph).Last())
mainPart.Document.Save()
wordDoc.Close()
End Using
counter = counter + 1
Next
End Sub

VB.NET Get directory and sub-directory from path

I'm trying to get the full path minus the filename from a path in vb.net. I'm using a textbox and a browse button to get the full path, but I want to save just the directories.
How do I get just the directories and sub-directories?
Example file name:
c:\Users\jsmith\Desktop\file.aspx
Desired result:
c:\Users\jsmith\Desktop\
Protected Sub btnBackupFolderName_Click(sender As Object, e As EventArgs) Handles btnBackupFolderName.Click
' Call ShowDialog.
Dim result As DialogResult = openFD.ShowDialog()
' Test result.
If result = Windows.Forms.DialogResult.OK Then
Dim FileNameText As String = openFD.FileName.ToString()
Dim backupFolderName = Path.GetFileName(Path.GetDirectoryName(FileNameText)) 'this just gives me 'Desktop'
txtBackupFolder.Text = di.ToString 'backupFolderName
End If
End Sub
Thank you for your help!
Get the full path of the file and then use GetDirectoryName() to retrieve the folder-path, like this:
Dim openFD As OpenFileDialog = New OpenFileDialog()
Dim result As DialogResult = openFD.ShowDialog()
If result = DialogResult.OK Then
Dim FileNameText As String = openFD.FileName.ToString()
' This gets the folder-path, sans filename.
txtBackupFolder.Text = Path.GetDirectoryName(openFD.FileName)
End If

create Xml Document from Node

Trying to create a new xml document from an existing xml node that is passed into a method. This with VB.NET. How to do ?
Private Shared Sub WriteChanges(parentNode As XmlNode, nodeName As String, m As Model.ModelBaseWithTracking)
Dim xml As New XmlDocument()
If parentNode.Name = "#document" Then
'Need code here
End If
End Sub
This is simple. You can create nodes in the XML document as following:
Private Shared Sub WriteChanges(parentNode As XmlNode, nodeName As String, m As Model.ModelBaseWithTracking)
Dim xml As New XmlDocument()
If parentNode.Name = "#document" Then
//To create root elemet
Dim root As XmlElement = xml.CreateElement("document")
xml.AppendChild(root)
//To add child node to root element
Dim child As XmlElement = xml.CreateElement("document1")
root.AppendChild(child)
child.SetAttribute("id", "1")
//Add more nodes same as shown above..
End If
End Sub

VBScript to add and delete row dynamcially

I would like to add a new row to my HTML table and delete the row dynamically using VBScript. Iam new to this can anyone guide me how to do this.
Note that this will be Internet Explorer specific. Might not work on other browsers/
Sub AddRow()
Dim objTable : Set objTable = window.document.getElementById("tableid")
Dim objRow : Set objRow = objTable.insertRow()
Dim intCount, objCell
For intCount = 0 To 2
Set objCell = objRow.insertCell()
objCell.innerHTML = "Content for cell")
Next
End Sub
For delete use
window.document.getElementById("tableid").deleteRow(oRow.rowIndex);

Lucene.net: Separate building Index from Searching the Index

I created a website but i have a problem.
i want to build once an index und use it.
at the moment i have two functions "create a document an store it into the directory" and "searching"
when the user submit:
sub submit ()
create_doc()
search(text)
end sub
this works, but when i try this:
create_doc()
sub submit()
search(text)
end sub
it's like the directory has been deleted.
global:
Dim analyzer As StandardAnalyzer = New StandardAnalyzer()Dim directory As Directory = FSDirectory.GetDirectory("C:\[...]luceneindex", True)
Dim indexwriter As IndexWriter = New IndexWriter(directory, analyzer, True)
Sub create_doc()
Dim meindoc As New Document()
im feldbodytext As Field = New Field("bodytext", textstring[...]
meindoc.Add(feldbodytext)
indexwriter.AddDocument(meindoc)
indexwriter.Close()
end sub
Sub lucene_search(ByVal strSuchbegriff As String)
Dim parser As QueryParser = New QueryParser("bodytext", analyzer)
Dim query As Query = parser.Parse(strSuchbegriff)
Dim hits As Hits = searcher.Search(query)
[...]
end sub
Is there a possibility to store the index permanently?
could there be a problem init. the index writer gloabel, but close it local?
I think your problem is that each time you declare your IndexWriter, the index is being re-created and the contents of the index erased - this is because of the 3rd parameter being passed into the constructor (True):
Dim indexwriter As IndexWriter = New IndexWriter(directory, analyzer, True)
You should instead use False, to indicate that the existing contents of the index should remain unchanged:
Dim indexwriter As IndexWriter = New IndexWriter(directory, analyzer, False)
ahh, i think i've got it ;-)
the first time i create a index i have to use
Dim directory As Directory = FSDirectory.GetDirectory("C:\[...]\luceneindex", True)
Dim indexwriter As IndexWriter = New IndexWriter("C:\[...]luceneindex", analyzer, True)
and after indexing i have to use both with "False".
True everytimes creates an index?
thanks =)

Resources