473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Output SiteMapNodeCollection as an XML file?

JJ
How can I get a SiteMapNodeCollection to output as an XML file?
Jul 11 '06 #1
10 2480
Could you clarify what you need to do? The SiteMapPath control reads from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
How can I get a SiteMapNodeCollection to output as an XML file?


Jul 12 '06 #2
JJ
I am constructing a Flash menu system that builds its content based on the web.sitemap file. How Flash does that here is of no importance - it just needs URL access to the XML sitemap file.
However, due to the built-in security, the server does not allow the '.sitemap' file to be accessed directly.

I am therefore writing a handler to allow access to the file. I could do this easily by reading in the physical file and outputting it.
But if I was to use roles, I did not want the Flash menu to have access to nodes which had roles which were more privileged than the current user.

I then came across 'security trimming', which if enabled, filters out the nodes that the user should see, but only when you use certain commands to access the web.sitemap nodes.
Reading the physical file was bypassing security trimming, and so I'd have had to hand code the roles filtering. I noticed that when I copied the current web.sitemap into a sitenodemapcollection it filtered out any nodes the user wasn't meant to see. This would save me having to come up with code to parse the web.sitemap file and do the filtering myself.

But, I am not sure how I output that collection in a form, presumably of:
Dim writer As XmlTextWriter = New XmlTextWriter(response.OutputStream, Encoding.UTF8)

Currently I am having to iterate through the web.sitemap file in the handler and manually check if there is a role string present and if currentuser.IsInRole(rolestring). But its messy and I want to use the built in security trimming.

I hope I haven't made that too confusing. I'm a bit new to asp.net,

JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message news:eY**************@TK2MSFTNGP03.phx.gbl...
Could you clarify what you need to do? The SiteMapPath control reads from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
>How can I get a SiteMapNodeCollection to output as an XML file?

Jul 12 '06 #3
Hi JJ,

Okay, I think I understand. Try the following code and see if it approaches
what you're after? You would need to point the Flash to the file
FlashSiteMap.ashx as if it were an XML file 'cause that's what it outputs.

Let us know?

Ken
Microsoft MVP [ASP.NET]
kj***@hotmail.com
<%@ WebHandler Language="VB" Class="FlashSiteMap" %>
' FlashSiteMap.ashx
' by Ken Cox [MVP]
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
' Gets the sitemap nodes and renders them
' via a handler to XML
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"

Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("Flashnodes", "http://pp/schemas/flashmap")

' Add root node
AddUrl(SiteMap.RootNode)

' Add all other nodes
Dim nodes As SiteMapNodeCollection = _
SiteMap.RootNode.GetAllNodes()
For Each node As SiteMapNode In nodes
AddUrl(node)
Next

writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Private Sub AddUrl(ByVal node As SiteMapNode)
' Open url tag
writer_xml.WriteStartElement("siteMapNode")
' Write location
writer_xml.WriteStartElement("url")
writer_xml.WriteString(node.Url)
writer_xml.WriteEndElement()
' Write description
writer_xml.WriteStartElement("description")
writer_xml.WriteString(node.Description)
writer_xml.WriteEndElement()

writer_xml.WriteStartElement("title")
writer_xml.WriteString(node.Title)
writer_xml.WriteEndElement()
' Close tag
writer_xml.WriteEndElement()
End Sub
Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:eW****************@TK2MSFTNGP05.phx.gbl...
I am constructing a Flash menu system that builds its content based on the
web.sitemap file. How Flash does that here is of no importance - it just
needs URL access to the XML sitemap file.
However, due to the built-in security, the server does not allow the
'.sitemap' file to be accessed directly.

I am therefore writing a handler to allow access to the file. I could do
this easily by reading in the physical file and outputting it.
But if I was to use roles, I did not want the Flash menu to have access to
nodes which had roles which were more privileged than the current user.

I then came across 'security trimming', which if enabled, filters out the
nodes that the user should see, but only when you use certain commands to
access the web.sitemap nodes.
Reading the physical file was bypassing security trimming, and so I'd have
had to hand code the roles filtering. I noticed that when I copied the
current web.sitemap into a sitenodemapcollection it filtered out any nodes
the user wasn't meant to see. This would save me having to come up with code
to parse the web.sitemap file and do the filtering myself.

But, I am not sure how I output that collection in a form, presumably of:
Dim writer As XmlTextWriter = New XmlTextWriter(response.OutputStream,
Encoding.UTF8)

Currently I am having to iterate through the web.sitemap file in the handler
and manually check if there is a role string present and if
currentuser.IsInRole(rolestring). But its messy and I want to use the built
in security trimming.

I hope I haven't made that too confusing. I'm a bit new to asp.net,

JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:eY**************@TK2MSFTNGP03.phx.gbl...
Could you clarify what you need to do? The SiteMapPath control reads from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
>How can I get a SiteMapNodeCollection to output as an XML file?



Jul 12 '06 #4
JJ
Ken,

Thanks - Thats _nearly_ what I want. It outputs all the nodes on one level
however, and splits up the attributes.
**Basically I need a way of making an identical copy of the sitemap, without
refering to the physcial file, or I'll bypass the security trimming.**

Do you lose the heirachical structure when you copy the sitemap to a
SiteMapNodeCollection?
If so I am on the wrong track completely.
Also, I need to allow for any custom attributes, like 'imageurl' for
example, so in my current code I use
writer.WriteAttributes(Reader, False) - but I guess as soon as you use an
XML reader it looks at the physical file and therefore bypasses security
trimming.
Would using some sort of Xpath structure do the trick? I did look at this,
but got confused...

JJ
"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:O9**************@TK2MSFTNGP03.phx.gbl...
Hi JJ,

Okay, I think I understand. Try the following code and see if it
approaches what you're after? You would need to point the Flash to the
file FlashSiteMap.ashx as if it were an XML file 'cause that's what it
outputs.

Let us know?

Ken
Microsoft MVP [ASP.NET]
kj***@hotmail.com
<%@ WebHandler Language="VB" Class="FlashSiteMap" %>
' FlashSiteMap.ashx
' by Ken Cox [MVP]
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
' Gets the sitemap nodes and renders them
' via a handler to XML
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"

Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("Flashnodes", "http://pp/schemas/flashmap")

' Add root node
AddUrl(SiteMap.RootNode)

' Add all other nodes
Dim nodes As SiteMapNodeCollection = _
SiteMap.RootNode.GetAllNodes()
For Each node As SiteMapNode In nodes
AddUrl(node)
Next

writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Private Sub AddUrl(ByVal node As SiteMapNode)
' Open url tag
writer_xml.WriteStartElement("siteMapNode")
' Write location
writer_xml.WriteStartElement("url")
writer_xml.WriteString(node.Url)
writer_xml.WriteEndElement()
' Write description
writer_xml.WriteStartElement("description")
writer_xml.WriteString(node.Description)
writer_xml.WriteEndElement()

writer_xml.WriteStartElement("title")
writer_xml.WriteString(node.Title)
writer_xml.WriteEndElement()
' Close tag
writer_xml.WriteEndElement()
End Sub
Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:eW****************@TK2MSFTNGP05.phx.gbl...
I am constructing a Flash menu system that builds its content based on the
web.sitemap file. How Flash does that here is of no importance - it just
needs URL access to the XML sitemap file.
However, due to the built-in security, the server does not allow the
'.sitemap' file to be accessed directly.

I am therefore writing a handler to allow access to the file. I could do
this easily by reading in the physical file and outputting it.
But if I was to use roles, I did not want the Flash menu to have access to
nodes which had roles which were more privileged than the current user.

I then came across 'security trimming', which if enabled, filters out the
nodes that the user should see, but only when you use certain commands to
access the web.sitemap nodes.
Reading the physical file was bypassing security trimming, and so I'd have
had to hand code the roles filtering. I noticed that when I copied the
current web.sitemap into a sitenodemapcollection it filtered out any nodes
the user wasn't meant to see. This would save me having to come up with
code to parse the web.sitemap file and do the filtering myself.

But, I am not sure how I output that collection in a form, presumably of:
Dim writer As XmlTextWriter = New XmlTextWriter(response.OutputStream,
Encoding.UTF8)

Currently I am having to iterate through the web.sitemap file in the
handler and manually check if there is a role string present and if
currentuser.IsInRole(rolestring). But its messy and I want to use the
built in security trimming.

I hope I haven't made that too confusing. I'm a bit new to asp.net,

JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:eY**************@TK2MSFTNGP03.phx.gbl...
>Could you clarify what you need to do? The SiteMapPath control reads from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
>>How can I get a SiteMapNodeCollection to output as an XML file?




Jul 12 '06 #5
Hi JJ,

Here's a version that uses recursion to loop through all the child nodes
while maintaining the attributes.

It should reproduce the security trimming for the current user.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ webhandler class="FlashSiteMap" language="VB" %>
' FlashSiteMap.ashx
' Gets the sitemap nodes and renders them
' via a handler to XML
' Uses recursion to write the
' childnodes for a given node
' Ken Cox [MVP] ASP.NET
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"
Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
settings.NewLineHandling = NewLineHandling.Entitize
settings.NewLineOnAttributes = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0")
'*******Recursive call here *******
RecurseSiteMapNode(SiteMap.RootNode.ChildNodes)
writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Sub RecurseSiteMapNode(ByVal nodes As SiteMapNodeCollection)
For Each newnode As SiteMapNode In nodes
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(newnode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(newnode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(newnode.Description)
writer_xml.WriteEndAttribute()
RecurseSiteMapNode(newnode.ChildNodes)
writer_xml.WriteEndElement()
Next
End Sub

Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
Ken,

Thanks - Thats _nearly_ what I want. It outputs all the nodes on one level
however, and splits up the attributes.
**Basically I need a way of making an identical copy of the sitemap,
without refering to the physcial file, or I'll bypass the security
trimming.**

Do you lose the heirachical structure when you copy the sitemap to a
SiteMapNodeCollection?
If so I am on the wrong track completely.
Also, I need to allow for any custom attributes, like 'imageurl' for
example, so in my current code I use
writer.WriteAttributes(Reader, False) - but I guess as soon as you use an
XML reader it looks at the physical file and therefore bypasses security
trimming.
Would using some sort of Xpath structure do the trick? I did look at this,
but got confused...

JJ
"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:O9**************@TK2MSFTNGP03.phx.gbl...
>Hi JJ,

Okay, I think I understand. Try the following code and see if it
approaches what you're after? You would need to point the Flash to the
file FlashSiteMap.ashx as if it were an XML file 'cause that's what it
outputs.

Let us know?

Ken
Microsoft MVP [ASP.NET]
kj***@hotmail.com
<%@ WebHandler Language="VB" Class="FlashSiteMap" %>
' FlashSiteMap.ashx
' by Ken Cox [MVP]
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
' Gets the sitemap nodes and renders them
' via a handler to XML
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"

Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("Flashnodes", "http://pp/schemas/flashmap")

' Add root node
AddUrl(SiteMap.RootNode)

' Add all other nodes
Dim nodes As SiteMapNodeCollection = _
SiteMap.RootNode.GetAllNodes()
For Each node As SiteMapNode In nodes
AddUrl(node)
Next

writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Private Sub AddUrl(ByVal node As SiteMapNode)
' Open url tag
writer_xml.WriteStartElement("siteMapNode")
' Write location
writer_xml.WriteStartElement("url")
writer_xml.WriteString(node.Url)
writer_xml.WriteEndElement()
' Write description
writer_xml.WriteStartElement("description")
writer_xml.WriteString(node.Description)
writer_xml.WriteEndElement()

writer_xml.WriteStartElement("title")
writer_xml.WriteString(node.Title)
writer_xml.WriteEndElement()
' Close tag
writer_xml.WriteEndElement()
End Sub
Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:eW****************@TK2MSFTNGP05.phx.gbl...
I am constructing a Flash menu system that builds its content based on
the web.sitemap file. How Flash does that here is of no importance - it
just needs URL access to the XML sitemap file.
However, due to the built-in security, the server does not allow the
'.sitemap' file to be accessed directly.

I am therefore writing a handler to allow access to the file. I could do
this easily by reading in the physical file and outputting it.
But if I was to use roles, I did not want the Flash menu to have access
to nodes which had roles which were more privileged than the current
user.

I then came across 'security trimming', which if enabled, filters out the
nodes that the user should see, but only when you use certain commands to
access the web.sitemap nodes.
Reading the physical file was bypassing security trimming, and so I'd
have had to hand code the roles filtering. I noticed that when I copied
the current web.sitemap into a sitenodemapcollection it filtered out any
nodes the user wasn't meant to see. This would save me having to come up
with code to parse the web.sitemap file and do the filtering myself.

But, I am not sure how I output that collection in a form, presumably
of:
Dim writer As XmlTextWriter = New XmlTextWriter(response.OutputStream,
Encoding.UTF8)

Currently I am having to iterate through the web.sitemap file in the
handler and manually check if there is a role string present and if
currentuser.IsInRole(rolestring). But its messy and I want to use the
built in security trimming.

I hope I haven't made that too confusing. I'm a bit new to asp.net,

JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:eY**************@TK2MSFTNGP03.phx.gbl...
>>Could you clarify what you need to do? The SiteMapPath control reads
from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
How can I get a SiteMapNodeCollection to output as an XML file?




Jul 13 '06 #6
JJ
I'll give it a try now - any way of adding all attributes without having to
specifiy each one?
JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:OK**************@TK2MSFTNGP04.phx.gbl...
Hi JJ,

Here's a version that uses recursion to loop through all the child nodes
while maintaining the attributes.

It should reproduce the security trimming for the current user.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ webhandler class="FlashSiteMap" language="VB" %>
' FlashSiteMap.ashx
' Gets the sitemap nodes and renders them
' via a handler to XML
' Uses recursion to write the
' childnodes for a given node
' Ken Cox [MVP] ASP.NET
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"
Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
settings.NewLineHandling = NewLineHandling.Entitize
settings.NewLineOnAttributes = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0")
'*******Recursive call here *******
RecurseSiteMapNode(SiteMap.RootNode.ChildNodes)
writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Sub RecurseSiteMapNode(ByVal nodes As SiteMapNodeCollection)
For Each newnode As SiteMapNode In nodes
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(newnode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(newnode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(newnode.Description)
writer_xml.WriteEndAttribute()
RecurseSiteMapNode(newnode.ChildNodes)
writer_xml.WriteEndElement()
Next
End Sub

Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
>Ken,

Thanks - Thats _nearly_ what I want. It outputs all the nodes on one
level however, and splits up the attributes.
**Basically I need a way of making an identical copy of the sitemap,
without refering to the physcial file, or I'll bypass the security
trimming.**

Do you lose the heirachical structure when you copy the sitemap to a
SiteMapNodeCollection?
If so I am on the wrong track completely.
Also, I need to allow for any custom attributes, like 'imageurl' for
example, so in my current code I use
writer.WriteAttributes(Reader, False) - but I guess as soon as you use an
XML reader it looks at the physical file and therefore bypasses security
trimming.
Would using some sort of Xpath structure do the trick? I did look at
this, but got confused...

JJ
"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:O9**************@TK2MSFTNGP03.phx.gbl...
>>Hi JJ,

Okay, I think I understand. Try the following code and see if it
approaches what you're after? You would need to point the Flash to the
file FlashSiteMap.ashx as if it were an XML file 'cause that's what it
outputs.

Let us know?

Ken
Microsoft MVP [ASP.NET]
kj***@hotmail.com
<%@ WebHandler Language="VB" Class="FlashSiteMap" %>
' FlashSiteMap.ashx
' by Ken Cox [MVP]
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
' Gets the sitemap nodes and renders them
' via a handler to XML
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"

Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("Flashnodes", "http://pp/schemas/flashmap")

' Add root node
AddUrl(SiteMap.RootNode)

' Add all other nodes
Dim nodes As SiteMapNodeCollection = _
SiteMap.RootNode.GetAllNodes()
For Each node As SiteMapNode In nodes
AddUrl(node)
Next

writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Private Sub AddUrl(ByVal node As SiteMapNode)
' Open url tag
writer_xml.WriteStartElement("siteMapNode")
' Write location
writer_xml.WriteStartElement("url")
writer_xml.WriteString(node.Url)
writer_xml.WriteEndElement()
' Write description
writer_xml.WriteStartElement("description")
writer_xml.WriteString(node.Description)
writer_xml.WriteEndElement()

writer_xml.WriteStartElement("title")
writer_xml.WriteString(node.Title)
writer_xml.WriteEndElement()
' Close tag
writer_xml.WriteEndElement()
End Sub
Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:eW****************@TK2MSFTNGP05.phx.gbl.. .
I am constructing a Flash menu system that builds its content based on
the web.sitemap file. How Flash does that here is of no importance - it
just needs URL access to the XML sitemap file.
However, due to the built-in security, the server does not allow the
'.sitemap' file to be accessed directly.

I am therefore writing a handler to allow access to the file. I could
do this easily by reading in the physical file and outputting it.
But if I was to use roles, I did not want the Flash menu to have access
to nodes which had roles which were more privileged than the current
user.

I then came across 'security trimming', which if enabled, filters out
the nodes that the user should see, but only when you use certain
commands to access the web.sitemap nodes.
Reading the physical file was bypassing security trimming, and so I'd
have had to hand code the roles filtering. I noticed that when I copied
the current web.sitemap into a sitenodemapcollection it filtered out any
nodes the user wasn't meant to see. This would save me having to come up
with code to parse the web.sitemap file and do the filtering myself.

But, I am not sure how I output that collection in a form, presumably
of:
Dim writer As XmlTextWriter = New XmlTextWriter(response.OutputStream,
Encoding.UTF8)

Currently I am having to iterate through the web.sitemap file in the
handler and manually check if there is a role string present and if
currentuser.IsInRole(rolestring). But its messy and I want to use the
built in security trimming.

I hope I haven't made that too confusing. I'm a bit new to asp.net,

JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:eY**************@TK2MSFTNGP03.phx.gbl...

Could you clarify what you need to do? The SiteMapPath control reads
from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
How can I get a SiteMapNodeCollection to output as an XML file?
>
>




Jul 13 '06 #7
JJ
It chops off the root node. I need that one as its displayed in the actual
web.sitemap.

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:OK**************@TK2MSFTNGP04.phx.gbl...
Hi JJ,

Here's a version that uses recursion to loop through all the child nodes
while maintaining the attributes.

It should reproduce the security trimming for the current user.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ webhandler class="FlashSiteMap" language="VB" %>
' FlashSiteMap.ashx
' Gets the sitemap nodes and renders them
' via a handler to XML
' Uses recursion to write the
' childnodes for a given node
' Ken Cox [MVP] ASP.NET
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"
Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
settings.NewLineHandling = NewLineHandling.Entitize
settings.NewLineOnAttributes = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0")
'*******Recursive call here *******
RecurseSiteMapNode(SiteMap.RootNode.ChildNodes)
writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Sub RecurseSiteMapNode(ByVal nodes As SiteMapNodeCollection)
For Each newnode As SiteMapNode In nodes
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(newnode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(newnode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(newnode.Description)
writer_xml.WriteEndAttribute()
RecurseSiteMapNode(newnode.ChildNodes)
writer_xml.WriteEndElement()
Next
End Sub

Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
>Ken,

Thanks - Thats _nearly_ what I want. It outputs all the nodes on one
level however, and splits up the attributes.
**Basically I need a way of making an identical copy of the sitemap,
without refering to the physcial file, or I'll bypass the security
trimming.**

Do you lose the heirachical structure when you copy the sitemap to a
SiteMapNodeCollection?
If so I am on the wrong track completely.
Also, I need to allow for any custom attributes, like 'imageurl' for
example, so in my current code I use
writer.WriteAttributes(Reader, False) - but I guess as soon as you use an
XML reader it looks at the physical file and therefore bypasses security
trimming.
Would using some sort of Xpath structure do the trick? I did look at
this, but got confused...

JJ
"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:O9**************@TK2MSFTNGP03.phx.gbl...
>>Hi JJ,

Okay, I think I understand. Try the following code and see if it
approaches what you're after? You would need to point the Flash to the
file FlashSiteMap.ashx as if it were an XML file 'cause that's what it
outputs.

Let us know?

Ken
Microsoft MVP [ASP.NET]
kj***@hotmail.com
<%@ WebHandler Language="VB" Class="FlashSiteMap" %>
' FlashSiteMap.ashx
' by Ken Cox [MVP]
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
' Gets the sitemap nodes and renders them
' via a handler to XML
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"

Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("Flashnodes", "http://pp/schemas/flashmap")

' Add root node
AddUrl(SiteMap.RootNode)

' Add all other nodes
Dim nodes As SiteMapNodeCollection = _
SiteMap.RootNode.GetAllNodes()
For Each node As SiteMapNode In nodes
AddUrl(node)
Next

writer_xml.WriteEndElement()
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Private Sub AddUrl(ByVal node As SiteMapNode)
' Open url tag
writer_xml.WriteStartElement("siteMapNode")
' Write location
writer_xml.WriteStartElement("url")
writer_xml.WriteString(node.Url)
writer_xml.WriteEndElement()
' Write description
writer_xml.WriteStartElement("description")
writer_xml.WriteString(node.Description)
writer_xml.WriteEndElement()

writer_xml.WriteStartElement("title")
writer_xml.WriteString(node.Title)
writer_xml.WriteEndElement()
' Close tag
writer_xml.WriteEndElement()
End Sub
Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:eW****************@TK2MSFTNGP05.phx.gbl.. .
I am constructing a Flash menu system that builds its content based on
the web.sitemap file. How Flash does that here is of no importance - it
just needs URL access to the XML sitemap file.
However, due to the built-in security, the server does not allow the
'.sitemap' file to be accessed directly.

I am therefore writing a handler to allow access to the file. I could
do this easily by reading in the physical file and outputting it.
But if I was to use roles, I did not want the Flash menu to have access
to nodes which had roles which were more privileged than the current
user.

I then came across 'security trimming', which if enabled, filters out
the nodes that the user should see, but only when you use certain
commands to access the web.sitemap nodes.
Reading the physical file was bypassing security trimming, and so I'd
have had to hand code the roles filtering. I noticed that when I copied
the current web.sitemap into a sitenodemapcollection it filtered out any
nodes the user wasn't meant to see. This would save me having to come up
with code to parse the web.sitemap file and do the filtering myself.

But, I am not sure how I output that collection in a form, presumably
of:
Dim writer As XmlTextWriter = New XmlTextWriter(response.OutputStream,
Encoding.UTF8)

Currently I am having to iterate through the web.sitemap file in the
handler and manually check if there is a role string present and if
currentuser.IsInRole(rolestring). But its messy and I want to use the
built in security trimming.

I hope I haven't made that too confusing. I'm a bit new to asp.net,

JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:eY**************@TK2MSFTNGP03.phx.gbl...

Could you clarify what you need to do? The SiteMapPath control reads
from
web.sitemap which is already an XML file.

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:uc**************@TK2MSFTNGP04.phx.gbl...
How can I get a SiteMapNodeCollection to output as an XML file?
>
>




Jul 13 '06 #8
Try this?

Ken
Microsoft MVP [ASP.NET]

<%@ webhandler class="FlashSiteMap" language="VB" %>
' FlashSiteMap.ashx
' Gets the root and sitemap nodes and renders them
' via a handler to XML
' Uses recursion to write the
' childnodes for a given node
' Ken Cox [MVP] ASP.NET
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"
Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
settings.NewLineHandling = NewLineHandling.Entitize
settings.NewLineOnAttributes = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0")
' Write the root node
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(SiteMap.RootNode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(SiteMap.RootNode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(SiteMap.RootNode.Descriptio n)
writer_xml.WriteEndAttribute()
' Write the child nodes
'*******Recursive call here *******
RecurseSiteMapNode(SiteMap.RootNode.ChildNodes)
' Finish the root node
writer_xml.WriteEndElement()
' Finish the start of the childnodes
writer_xml.WriteEndElement()
' Finish the document
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Sub RecurseSiteMapNode(ByVal nodes As SiteMapNodeCollection)
For Each newnode As SiteMapNode In nodes
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(newnode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(newnode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(newnode.Description)
writer_xml.WriteEndAttribute()
RecurseSiteMapNode(newnode.ChildNodes)
writer_xml.WriteEndElement()
Next
End Sub

Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:u%****************@TK2MSFTNGP05.phx.gbl...
It chops off the root node. I need that one as its displayed in the actual
web.sitemap.


Jul 13 '06 #9
I'm still looking at automating that. If you have custom attributes, it
looks like this is supported:

writer_xml.WriteString(newnode.Item("customattribu te"))

Ken
Microsoft MVP [ASP.NET]

"JJ" <ab*@xyz.comwrote in message
news:Oy*************@TK2MSFTNGP05.phx.gbl...
I'll give it a try now - any way of adding all attributes without having
to specifiy each one?
JJ

Jul 13 '06 #10
JJ
Ken that works, but I still am puzzled over how to get the unspecified
number of custom attributes.
I notice there is a SiteMapNode.Attributes Property - but it says its
'protected' and I can't figure out how I can use it?
JJ

"Ken Cox [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Try this?

Ken
Microsoft MVP [ASP.NET]

<%@ webhandler class="FlashSiteMap" language="VB" %>
' FlashSiteMap.ashx
' Gets the root and sitemap nodes and renders them
' via a handler to XML
' Uses recursion to write the
' childnodes for a given node
' Ken Cox [MVP] ASP.NET
' Adapted from Stephen Walther's code
' in ASP.NET 2.0 Unleashed, Chapter 18
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class FlashSiteMap
Implements IHttpHandler
Private writer_xml As XmlWriter

Public Sub ProcessRequest _
(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/xml"
Dim settings As New XmlWriterSettings()
settings.Encoding = Encoding.UTF8
settings.Indent = True
settings.NewLineHandling = NewLineHandling.Entitize
settings.NewLineOnAttributes = True
writer_xml = XmlWriter.Create _
(context.Response.OutputStream, settings)
writer_xml.WriteStartDocument()
writer_xml.WriteStartElement _
("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0")
' Write the root node
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(SiteMap.RootNode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(SiteMap.RootNode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(SiteMap.RootNode.Descriptio n)
writer_xml.WriteEndAttribute()
' Write the child nodes
'*******Recursive call here *******
RecurseSiteMapNode(SiteMap.RootNode.ChildNodes)
' Finish the root node
writer_xml.WriteEndElement()
' Finish the start of the childnodes
writer_xml.WriteEndElement()
' Finish the document
writer_xml.WriteEndDocument()
writer_xml.Flush()
End Sub

Sub RecurseSiteMapNode(ByVal nodes As SiteMapNodeCollection)
For Each newnode As SiteMapNode In nodes
writer_xml.WriteStartElement("siteMapNode")
writer_xml.WriteStartAttribute("url")
writer_xml.WriteString(newnode.Url)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("title")
writer_xml.WriteString(newnode.Title)
writer_xml.WriteEndAttribute()
writer_xml.WriteStartAttribute("description")
writer_xml.WriteString(newnode.Description)
writer_xml.WriteEndAttribute()
RecurseSiteMapNode(newnode.ChildNodes)
writer_xml.WriteEndElement()
Next
End Sub

Public ReadOnly Property IsReusable() _
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
"JJ" <ab*@xyz.comwrote in message
news:u%****************@TK2MSFTNGP05.phx.gbl...
>It chops off the root node. I need that one as its displayed in the
actual web.sitemap.



Jul 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
4
by: ed | last post by:
Hi all, I'm very new to vb (2nd day) and I need to create a small app that will replace my old batch file with a flashy gui. I had some experience with access 2.0 which helps ;) What I would...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
1
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are...
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.