Last post Apr 23, 2008 12:27 AM by Westnyorai
Apr 21, 2008 11:40 PM|Westnyorai|LINK
I've been playing around with URL rewriting lately, and found out a lot of very useful app mapping tools along with global.asax.
What I'm thinking of doing is actually using a URL rewrite method for challenging the web.sitemap xml file. My issue is most of the content of my page is controled through a database, so I don't have many static pages at all. The problem is that I want
to include the database entries into the web.sitemap file so that I can update the XML whenever someone request the information to input all the database entries within the right sections.
I can already setup everything so that the .sitemap will be redirected to .net 2.0 and altered so it will show dynamic content, but where i'm struggling is getting into XML and learning how to change a specific node.
<siteMapNode title="Home" url="/">
<siteMapNode title="Search" url="/search/" />
<siteMapNode title="Title" url="/title/">
<!-- This is where I want to input database entries such as:
<siteMapNode title="Title1" url="/title/1.info" />
<siteMapNode title="Title2" url="/title/2.info" />... etc -->
How do I do a search to find the siteMapNode for "Title" then go ahead and remove all current nodes, then go in and add new nodes? Even if you have some good references, preferably VB.Net but I can decode C# if needed.
Also I want to do sitemap because I want to sort of trick the searchbots in a way to finding the entries a bit better, and so I can include a treeview node, somewhat like MSDN has.
Apr 22, 2008 01:42 PM|Samu Zhang - MSFT|LINK
Hi Westnyorai ,
You can use XmlDocument to load the web.sitemap file , then you can pick out the node by using xpath. Then you can insert new nodes to it or remove child nodes from it.
learn xpath :
example about how to edit xml file:
Apr 23, 2008 12:27 AM|Westnyorai|LINK
Hey thanks for the help. I was able to figure it out once I get the basics down. lol Good old MSDN never lets you down.
Anyway for those who end up being stuck like I was here's the code: (Note: This is only the XML part)
Dim myxmlDoc as new XmlDocument()
Dim myNode as XmlNode = myxmlDoc.SelectSingleNode("/siteMap/siteMapNode/siteMapNode[attribute::title='Titles']")
'Remove Child Nodes
myNode.innerText = ""
'Var for holding new nodes
Dim elem As XmlElement
'Connect to database
Dim adoCmdStr As String = "select title,title_id from titles;"
Dim adoConnStr As String = ConfigurationSettings.AppSettings("--Insert Connection String--")
Dim adoConn As OdbcConnection = New OdbcConnection(AdoConnStr)
Dim adoDApt As OdbcDataAdapter = New OdbcDataAdapter(adoCmdStr, adoConn)
Dim adoCmd As OdbcCommand= New OdbcCommand(adoCmdStr, adoConn)
Dim adoRdr As OdbcDataReader
adoRdr = adoCmd.ExecuteReader(CommandBehavior.CloseConnection)
'data reader reads all rows
elem = myxmlDoc.CreateElement("siteMapNode")
elem.SetAttribute("url","/anime/" & adoRdr.GetValue(adoRdr.GetOrdinal("Title_ID")).toString & ".info")
'appends new node
It's a lot easier than expected.