?
Solved

change xml layout and Hashtable

Posted on 2009-02-24
14
Medium Priority
?
653 Views
Last Modified: 2013-12-17
My xml file is presently structured like this:
<Records>
  <Record>
    <ID>Open</ID>
    <Data>blah blah text blah blah text blah blah text blah blah let's get 'er done!</Data>
  </Record>
  <Record>
    <ID>OpenResponse</ID>
    <Data>Well, blah blah blah. This needs to get done asap. Blah blah </Data>
  </Record>
</Records>
-----#########BUT I WANT TO MAKE IT LIKE THIS INSTEAD########-------
<Scripts>
 <Script Desc="Open" Value="blah blah text blah blah text blah blah text blah blah let's get 'er done! "/>
<Script Desc="OpenResponse" Value="Well, blah blah blah. This needs to get done asap. Blah blah "/>
</Scripts>
---####BUT MY PROBLEM IS HOW TO RE-WRITE THIS CODE TO WORK WITH NEW XML LAYOUT####---
Any Expert that can explain how to re-write my snippet below to work with the new xml layout? Thanks.
Partial Class Begin
    Inherits System.Web.UI.Page
    Dim hashTableData As New Hashtable
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("Customer") = "Mr. Mcgillicutty"
        If Not Page.IsPostBack Then
            'Reading xml file
            Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
            doc.Load(Server.MapPath("XMLFile.xml"))
            Dim list As System.Xml.XmlNodeList = doc.SelectNodes("Records/Record")
            For Each node As System.Xml.XmlNode In list
                hashTableData(node.Item("ID").FirstChild.Value) = node.Item("Data").FirstChild.Value
            Next
            ViewState("Records") = hashTableData
        End If
    End Sub
 
    Protected Sub rdoIntroRebuttles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoIntroRebuttles.SelectedIndexChanged
        btnEdit.Visible = True
        Dim myTable As Hashtable = CType(ViewState("Records"), Hashtable)
        Select Case rdoIntroRebuttles.SelectedItem.Value
            Case "0"
                ViewState("HashKey") = "Rebuttle1"
                lbl1.Text = CType(ViewState("Records"), Hashtable)("Rebuttle1") & ", " & Session("Customer")
            Case "1"
                ViewState("HashKey") = "Rebuttle21"
                lbl1.Text = myTable("Rebuttle21") & ", " & Session("Customer")
            Case "2"
                ViewState("HashKey") = "Rebuttle31"
                lbl1.Text = myTable("Rebuttle31") & ", " & Session("Customer") & ", " & CType(ViewState("Records"), Hashtable)("Rebuttle32")
            Case "3"
                ViewState("HashKey") = "Rebuttle4"
                lbl1.Text = myTable("Rebuttle4") & ", " & Session("Customer") & "."
            Case "4"
                ViewState("HashKey") = "Rebuttle51"
                lbl1.Text = CType(ViewState("Records"), Hashtable)("Rebuttle51") & ", " & Session("Customer") & ".  " & CType(ViewState("Records"), Hashtable)("Rebuttle52")
            Case "5"
                ViewState("HashKey") = "Rebuttle61"
                lbl1.Text = CType(ViewState("Records"), Hashtable)("Rebuttle61") & ", " & Session("Customer") & ". " & CType(ViewState("Records"), Hashtable)("Rebuttle62")
            Case "6"
                ViewState("HashKey") = "Rebuttle71"
                lbl1.Text = CType(ViewState("Records"), Hashtable)("Rebuttle71") + ", " & Session("Customer") & ". " + CType(ViewState("Records"), Hashtable)("Rebuttle72")
        End Select
    End Sub
 
    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Me.txtEdit.Text = Me.lbl1.Text
        Me.txtEdit.Visible = True
        Me.Buttonokay.Visible = True
    End Sub
 
    Protected Sub Buttonokay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Buttonokay.Click
        Me.ModifyRecord()
    End Sub
 
    Private Sub ModifyRecord()
        Dim key As String = ViewState("HashKey")
        CType(ViewState("Records"), Hashtable)(key) = Me.txtEdit.Text
        Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        doc.Load(Server.MapPath("XMLFile.xml")) 
        Dim myNode As System.Xml.XmlNode = doc.SelectSingleNode("Records/Record[ID='" & key & "']")
        myNode("Data").InnerText = Me.txtEdit.Text 
        doc.Save(Server.MapPath("XMLFile.xml"))
        Me.lbl1.Text = Me.txtEdit.Text 
        Me.txtEdit.Visible = False
        Me.Buttonokay.Visible = False
    End Sub
 
End Class

Open in new window

0
Comment
Question by:John Account
  • 7
  • 7
14 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 23723111
Here's one way that worked on my machine:

Dim xmlString As String = "<?xml version=""1.0"" encoding=""UTF-8"" ?><Scripts><Script Desc=""Open"" Value=""blah blah text blah blah text blah blah text blah blah let's get 'er done! ""/><Script Desc=""OpenResponse"" Value=""Well, blah blah blah. This needs to get done asap. Blah blah ""/></Scripts>"
Dim xDoc As New XmlDocument()
xDoc.LoadXml(xmlString)
Dim root As XmlNode = xDoc.DocumentElement
Dim nodeList As XmlNodeList = root.SelectNodes("descendant::Script")
 
If nodeList IsNot Nothing Then
    
    For i As Integer = 0 To nodeList.Count - 1
        Dim listOrder As XmlNode = nodeList(i)
        
        For j As Integer = 0 To listOrder.Attributes.Count - 1
            If listOrder.Attributes(j).Name = "Desc" Then
                Dim scriptDesc As String = listOrder.Attributes(j).InnerText
            ElseIf listOrder.Attributes(j).Name = "Value" Then
                Dim scriptValue As String = listOrder.Attributes(j).InnerText
            End If
        Next
    Next
End If

Open in new window

0
 

Author Comment

by:John Account
ID: 23724466
Hmmmmnnnn...thanks, but that's not going to work for me, because I need to keep the xml data in an xml file on the server. The way you suggested is to hard code it into the code behind. There's no way I can get it from a file?
0
 
LVL 27

Expert Comment

by:nmarun
ID: 23724491

Dim xmlFile As String = "[set the xml file path here]";
Dim xDoc As New XmlDocument()
xDoc.Load(xmlFile);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:John Account
ID: 23724646
Hmmmmnnnn...getting lots of errors:
Compiler Error Message: BC30002: Type 'XmlDocument' is not defined.
Type 'XmlDocument' is not defined
Type 'XmlNode' is not defined
Type 'XmlNodeList' is not defined

Is my attached Code Snippet what you've suggested I try?
Dim xmlFile As String = "~/App_Data/XMLFileRedesigned.xml"
        Dim xDoc As New XmlDocument()
        xDoc.Load(xmlFile)
        Dim root As XmlNode = xDoc.DocumentElement
        Dim nodeList As XmlNodeList = root.SelectNodes("descendant::Script")
 
        If nodeList IsNot Nothing Then
 
            For i As Integer = 0 To nodeList.Count - 1
                Dim listOrder As XmlNode = nodeList(i)
 
                For j As Integer = 0 To listOrder.Attributes.Count - 1
                    If listOrder.Attributes(j).Name = "Desc" Then
                        Dim scriptDesc As String = listOrder.Attributes(j).InnerText
                    ElseIf listOrder.Attributes(j).Name = "Value" Then
                        Dim scriptValue As String = listOrder.Attributes(j).InnerText
                    End If
                Next
            Next
        End If

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 23724659
Please import the System.Xml namespace.
0
 

Author Comment

by:John Account
ID: 23724721
Ooops...forgot that. But now I'm getting this error: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. And I don' think I know what that's all about. You?
0
 
LVL 27

Expert Comment

by:nmarun
ID: 23724756
That's just saying that the ASP.NET user does not have permissions to access that folder/file. Have a look at these links:

http://forums.asp.net/p/1257667/2360353.aspx
http://www.velocityreviews.com/forums/t295686-fileiopermission-erro-using-referenced-dll-on-shared-hosting.html
http://www.eggheadcafe.com/software/aspnet/29984362/fileio-exception-and-perm.aspx (the solutions are displayed in links at the bottom)
0
 

Author Comment

by:John Account
ID: 23724765
WIth your solution, I don't see anythign where I can reference the ViewState and hashtable data. Maybe that's the problem. See attached snippet, of my previous code.
        If Not Page.IsPostBack Then
            'Reading xml file 
            Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
            doc.Load(Server.MapPath("XMLFile.xml"))
            Dim list As System.Xml.XmlNodeList = doc.SelectNodes("Records/Record")
            For Each node As System.Xml.XmlNode In list
                hashTableData(node.Item("ID").FirstChild.Value) = node.Item("Data").FirstChild.Value
            Next
            ViewState("Records") = hashTableData
        End If

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 23724816
No, it's not the ViewState that's causing this error. You can still add the entries to a hashtable and store them in ViewState.
0
 

Author Comment

by:John Account
ID: 23725220
Well, I'm not getting anywhere with this. And it's against TOS to offer to pay you, right? Sigh...guess, I'm stuck here. Hopefully, I'll be able to see the bigger picture soon.
0
 
LVL 27

Expert Comment

by:nmarun
ID: 23725315
John,

I thought you were getting close. Did you provide permissions to the file/folder? As far as adding the hashtable to the ViewState, it seemed like you kinda figured it out yourself.

I read through my posts again and I don't think I ever hinted you to pay me to get this to work.

If you're not satisfied with what I've posted.. I'll keep myself off of posting in this thread and the moderators will eventually have a look at it and decide how to go about it.

Let me know.
Arun
0
 

Author Comment

by:John Account
ID: 23725467
No, no...sorry, I didnt' mean it like that at all. Was thinking outloud, wishing I could pay you to help. But, sorry, just stressed--nevermind, please don't read into that. I just need to get my mind out of a box and try to figure this thing out. As for file/folder permissions, not on my local machine, but yes, have it on my server. Immediate problem, though is with hashtable and viewstate. And I'd really like to figure this out, because the way I edit the xml files, I need the xml files in a certain order. You can see this application in it's present state here: http://www.versacore.info/Telemarketing/  If you look at my menu bar, Edit/All (main), you see that only Selection List/Demographics and /Rebuttals are editable. That's because everything else is using the other xml structure, which is what I'm trying to figure out how to change over.
0
 

Author Closing Comment

by:John Account
ID: 31550602
By the way, I'm sorry if I offended you. Your help was superb. My mind is just in a box a bit right now.
0
 
LVL 27

Expert Comment

by:nmarun
ID: 23725727
To add to the hashTable and eventually to the ViewState, try this:

Dim xmlFile As String = "~/App_Data/XMLFileRedesigned.xml"
Dim xDoc As New XmlDocument()
xDoc.Load(xmlFile)
Dim root As XmlNode = xDoc.DocumentElement
Dim nodeList As XmlNodeList = root.SelectNodes("descendant::Script")
 
If nodeList IsNot Nothing Then
    For i As Integer = 0 To nodeList.Count - 1
        Dim listOrder As XmlNode = nodeList(i)
        
        Dim scriptDesc As String = String.Empty
        Dim scriptValue As String = String.Empty
        Dim hashtable As New Hashtable()
        For j As Integer = 0 To listOrder.Attributes.Count - 1
            If listOrder.Attributes(j).Name = "Desc" Then
                scriptDesc = listOrder.Attributes(j).InnerText
            ElseIf listOrder.Attributes(j).Name = "Value" Then
                scriptValue = listOrder.Attributes(j).InnerText
                ' if the desc does not have a value
                ' don't add it to the hashtable; it'll break
                If scriptDesc <> String.Empty Then
                    hashtable.Add(scriptDesc, scriptValue)
                    
                    ' reset the values of desc and value to an empty string
                    scriptDesc = String.Empty
                    scriptValue = String.Empty
                End If
            End If
        Next
        
        ViewState("Records") = hashtable
    Next
End If

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question