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.
Example:
<siteMap>
<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 -->
</siteMapNode>
</siteMapNode>
</siteMap>
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.
Please remember to mark the post(s) that provided your answer. Also, please don't paste insane amounts of code and use short paragraphs. That and remember it may take a few days to get a response.
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.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
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()
myxmlDoc.Load(Server.MapPath("web.sitemap"))
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
adoConn.Open()
adoRdr = adoCmd.ExecuteReader(CommandBehavior.CloseConnection)
'data reader reads all rows
While adoRdr.Read()
elem = myxmlDoc.CreateElement("siteMapNode")
elem.SetAttribute("title",adoRdr.GetValue(adoRdr.GetOrdinal("Title")).toString)
elem.SetAttribute("url","/anime/" & adoRdr.GetValue(adoRdr.GetOrdinal("Title_ID")).toString & ".info")
'appends new node
myNode.AppendChild(elem)
End While
'Saves XML
myxmlDoc.Save(Server.MapPath("web.sitemap"))
It's a lot easier than expected.
Please remember to mark the post(s) that provided your answer. Also, please don't paste insane amounts of code and use short paragraphs. That and remember it may take a few days to get a response.
Westnyorai
Member
206 Points
106 Posts
dynamic web.sitemap, altering XML upon request
Apr 21, 2008 11:40 PM|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.
Example:
<siteMap> <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 --> </siteMapNode> </siteMapNode> </siteMap>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.
Samu Zhang -...
All-Star
62163 Points
6101 Posts
Re: dynamic web.sitemap, altering XML upon request
Apr 22, 2008 01:42 PM|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 :
http://www.w3schools.com/xpath/default.asp
example about how to edit xml file:
http://www.codeproject.com/KB/aspnet/Edit_Xml.aspx
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Westnyorai
Member
206 Points
106 Posts
Re: dynamic web.sitemap, altering XML upon request
Apr 23, 2008 12:27 AM|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() myxmlDoc.Load(Server.MapPath("web.sitemap")) 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 adoConn.Open() adoRdr = adoCmd.ExecuteReader(CommandBehavior.CloseConnection) 'data reader reads all rows While adoRdr.Read() elem = myxmlDoc.CreateElement("siteMapNode") elem.SetAttribute("title",adoRdr.GetValue(adoRdr.GetOrdinal("Title")).toString) elem.SetAttribute("url","/anime/" & adoRdr.GetValue(adoRdr.GetOrdinal("Title_ID")).toString & ".info") 'appends new node myNode.AppendChild(elem) End While 'Saves XML myxmlDoc.Save(Server.MapPath("web.sitemap"))It's a lot easier than expected.