473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add parent node in Vb.NET

Hi,

I am having an XML document(input. xml) which is showing the menu heirarchy
used in my application.My requirement is to add "submenu" tag to those
menuitems who are having the child menuitems as shown in output.xml.
I am using VB.NET framework 1.1.

Please Help, i have already invested my 2 days but unable to build the logic
as input xml may be having any level of nesting of menuitems.

input.xml
*************** *************** *********
<menu>
<menuItem>
<text>HOME</text>
<javascriptcomm and>opennewwind ow('home.aspx') </javascriptcomma nd>
<cssclass>off </cssclass>
<MENU_ID>1</MENU_ID>
</menuItem>
<menuItem>
<text>QUERY</text>
<javascriptcomm and>openPopup(' fundSearch.aspx ','Search', 580,
200)</javascriptcomma nd>
<cssclass>off </cssclass>
<MENU_ID>2</MENU_ID>
</menuItem>
<menuItem>
<text>REPORTS </text>
<cssclass>off </cssclass>
<MENU_ID>3</MENU_ID>
<menuItem>
<text>Standar d Reports</text>
<MENU_ID>5</MENU_ID>
<PRNT_MENU_ID>3 </PRNT_MENU_ID>
<menuItem>
<text>PM Tenure</text>
<MENU_ID>7</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Performan ce</text>
<MENU_ID>8</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fee & Expenses</text>
<MENU_ID>9</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fact Sheet PDFs</text>
<MENU_ID>10</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fund Facts</text>
<MENU_ID>11</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
</menuItem>
<menuItem>
<text>Custom Reports</text>
<MENU_ID>6</MENU_ID>
<PRNT_MENU_ID>3 </PRNT_MENU_ID>
<menuItem>
<text>Spectrums </text>
<MENU_ID>12</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Performan ce</text>
<MENU_ID>13</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Descripti ons</text>
<MENU_ID>14</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fund Actions</text>
<MENU_ID>15</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Inv Svcs Only</text>
<MENU_ID>16</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
</menuItem>
</menuItem>
<menuItem>
<text>HELP</text>
<cssclass>off </cssclass>
<MENU_ID>4</MENU_ID>
<menuItem>
<text>FAQs</text>
<MENU_ID>17</MENU_ID>
<PRNT_MENU_ID>4 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>How To</text>
<MENU_ID>18</MENU_ID>
<PRNT_MENU_ID>4 </PRNT_MENU_ID>
</menuItem>
</menuItem>
</menu>

output.xml
*************** *************** *************** ********
<menu>
<menuItem>
<text>HOME</text>
<javascriptcomm and>opennewwind ow('home.aspx') </javascriptcomma nd>
<cssclass>off </cssclass>
<MENU_ID>1</MENU_ID>
</menuItem>
<menuItem>
<text>QUERY</text>
<javascriptcomm and>openPopup(' fundSearch.aspx ','Search', 580,
200)</javascriptcomma nd>
<cssclass>off </cssclass>
<MENU_ID>2</MENU_ID>
</menuItem>
<menuItem>
<text>REPORTS </text>
<cssclass>off </cssclass>
<MENU_ID>3</MENU_ID>
<submenu>
<menuItem>
<text>Standar d Reports</text>
<MENU_ID>5</MENU_ID>
<PRNT_MENU_ID>3 </PRNT_MENU_ID>
<submenu>
<menuItem>
<text>PM Tenure</text>
<MENU_ID>7</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Performan ce</text>
<MENU_ID>8</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fee & Expenses</text>
<MENU_ID>9</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fact Sheet PDFs</text>
<MENU_ID>10</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fund Facts</text>
<MENU_ID>11</MENU_ID>
<PRNT_MENU_ID>5 </PRNT_MENU_ID>
</menuItem>
</submenu>
</menuItem>
<menuItem>
<text>Custom Reports</text>
<MENU_ID>6</MENU_ID>
<PRNT_MENU_ID>3 </PRNT_MENU_ID>
<submenu>
<menuItem>
<text>Spectrums </text>
<MENU_ID>12</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Performan ce</text>
<MENU_ID>13</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Descripti ons</text>
<MENU_ID>14</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Fund Actions</text>
<MENU_ID>15</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>Inv Svcs Only</text>
<MENU_ID>16</MENU_ID>
<PRNT_MENU_ID>6 </PRNT_MENU_ID>
</menuItem>
</submenu>
</menuItem>
</submenu>
</menuItem>
<menuItem>
<text>HELP</text>
<cssclass>off </cssclass>
<MENU_ID>4</MENU_ID>
<submenu>
<menuItem>
<text>FAQs</text>
<MENU_ID>17</MENU_ID>
<PRNT_MENU_ID>4 </PRNT_MENU_ID>
</menuItem>
<menuItem>
<text>How To</text>
<MENU_ID>18</MENU_ID>
<PRNT_MENU_ID>4 </PRNT_MENU_ID>
</menuItem>
</submenu>
</menuItem>
</menu>

Thanks & Regards,
Neeraj
Mar 22 '06
14 11506
Hi,

I am facing one problem with the given code:

When i debug the code through breakpoints it is givng me the Output XML
Correctly. but when i running the code without debugging it fails to add in
some of last menuitems..

Can you please check this code on your machine.just provide input xml as the
input it will save output.xml on your C: drive.

Please help.

Thanks in Advance.

Regards,
Neeraj

Private Sub createXMLFile(B yVal xmlstring As String)
Dim objDoc As New System.Xml.XmlD ocument
objDoc.LoadXml( xmlstring)
Dim objmenuItemNode List As System.Xml.XmlN odeList
Dim objMenuNode As System.Xml.XmlN ode
Dim menuitems As ArrayList
Dim counter As Integer
objmenuItemNode List = objDoc.SelectNo des("//menuItem")

For Each objMenuNode In objmenuItemNode List

If (checkChildMenu Items(objMenuNo de)) Then
Dim objSubMenuNode As System.Xml.XmlN ode =
objDoc.CreateNo de(System.Xml.X mlNodeType.Elem ent, "subMenu", "")

menuitems = menuItemsArrayL ist(objMenuNode )
For counter = 0 To menuitems.Count - 1

objSubMenuNode. AppendChild(Dir ectCast(menuite ms(counter), XmlNode))
Next
menuitems = Nothing
objMenuNode.App endChild(objSub MenuNode)
End If
Next
Dim xmldecl As XmlDeclaration
xmldecl = objDoc.CreateXm lDeclaration("1 .0", "utf-8", String.Empty)
objRootNode = objDoc.Document Element
objDoc.InsertBe fore(xmldecl, objRootNode)
objDoc.Save("C: \outputXML.xml" )
objDoc = Nothing
End Sub

Private Function checkChildMenuI tems(ByVal sampleNode As XmlNode) As Integer
Dim returnvalue As Boolean
Dim i As Integer
returnvalue = False
For i = 0 To sampleNode.Chil dNodes.Count - 1
If (sampleNode.Chi ldNodes(i).Name ) = "menuItem" Then
returnvalue = True
Exit For
End If
Next
checkChildMenuI tems = returnvalue
End Function
Private Function menuItemsArrayL ist(ByVal sampleNode As XmlNode) As
ArrayList
Dim returnvalue As New ArrayList
Dim i As Integer
For i = 0 To sampleNode.Chil dNodes.Count - 1
If (sampleNode.Chi ldNodes(i).Name ) = "menuItem" Then
returnvalue.Add (sampleNode.Chi ldNodes(i))
End If
Next
menuItemsArrayL ist = returnvalue
End Function

"ne*****@noida. nospamhcltech.c om" wrote:
Hi Cerebrus,
Thanks for the reply.
Your understandign perfectly correct.
The logic which we have to apply is we have to insert subMenu tag whenever
menuItem is having the child menuItem and all the child menuItem tags should
be enclosed in subMenu tag.
Like in my input xml menuItem "REPORTS" is having two child meuItems
Standard Reports and Custom Reports. so they should be come into "subMenu"
tag. Also Now Custom Reports is also having 5 menuItem childs, so they should
be enclosed in one SubMenu Tag.

It's very simple that Reports is top level Menu
Standard Reports and Custom Reports are second level menu
Child under Standard reports are third level menu

You can see in my output xml
Currently i am using the following logic but i am not sure will it work for
any level of nesting

please help in providing the bugproof logic.

For Each objMenuNode In objmenuItemNode List
If (checkChildMenu Items(objMenuNo de)) Then
Dim objSubMenuNode As System.Xml.XmlN ode =
objDoc.CreateNo de(System.Xml.X mlNodeType.Elem ent, "subMenu", "")
menuitems = menuItemsArrayL ist(objMenuNode )
For counter = 0 To menuitems.Count - 1

objSubMenuNode. AppendChild(Dir ectCast(menuite ms(counter), XmlNode))
Next
objMenuNode.App endChild(objSub MenuNode)
End If
Next

*************** *************** *************** *************** *************** *************** ***
Private Function checkChildMenuI tems(ByVal sampleNode As XmlNode) As Integer
Dim returnvalue As Boolean
Dim i As Integer
returnvalue = False
For i = 0 To sampleNode.Chil dNodes.Count - 1
If (sampleNode.Chi ldNodes(i).Name ) = "menuItem" Then
returnvalue = True
Exit For
End If
Next
checkChildMenuI tems = returnvalue
End Function

*************** *************** *************** *************** *************** *************** *****
Private Function menuItemsArrayL ist(ByVal sampleNode As XmlNode) As
ArrayList
Dim returnvalue As New ArrayList
Dim i As Integer
For i = 0 To sampleNode.Chil dNodes.Count - 1
If (sampleNode.Chi ldNodes(i).Name ) = "menuItem" Then
returnvalue.Add (sampleNode.Chi ldNodes(i))
End If
Next
menuItemsArrayL ist = returnvalue
End Function
"Cerebrus" wrote:
Hi,

I'm not sure I understand you correctly, but I think you need to insert
a <subMenu> element before each nested <menuItem> element. In other
words, you want to convert input.xml so that it looks like output.xml
???

Is that so ? If so, could you post whatever logic you have come up with
till now ?

Regards,

Cerebrus.

Mar 24 '06 #11
Hi Cerebrus,

I am facing some new problem in the undermentioned code:

When i am running it in debug mode it's giving me correct Output CML with
submenu tags properly inserted but when i am runnign it with out debugging
it's skipping some of the last menuitems for adding submenus.

is it the memory issue..

just try to run this code on your machine and check in debugging as well as
without debugging you can understand the differences...

Just provide the input xml as as the input to the function.

Please help.I have already invested lot of the time in the issue but unable
to understand it.

Neeraj

*************** *************** ***************

Private Sub createXMLFile(B yVal xmlstring As String)
Dim objDoc As New System.Xml.XmlD ocument
objDoc.LoadXml( xmlstring)
Dim objmenuItemNode List As System.Xml.XmlN odeList
Dim objMenuNode As System.Xml.XmlN ode
Dim menuitems As ArrayList
Dim counter As Integer
objmenuItemNode List = objDoc.SelectNo des("//menuItem")

For Each objMenuNode In objmenuItemNode List

If (checkChildMenu Items(objMenuNo de)) Then
Dim objSubMenuNode As System.Xml.XmlN ode =
objDoc.CreateNo de(System.Xml.X mlNodeType.Elem ent, "subMenu", "")

menuitems = menuItemsArrayL ist(objMenuNode )
For counter = 0 To menuitems.Count - 1

objSubMenuNode. AppendChild(Dir ectCast(menuite ms(counter), XmlNode))
Next
menuitems = Nothing
objMenuNode.App endChild(objSub MenuNode)
End If
Next
Dim xmldecl As XmlDeclaration
xmldecl = objDoc.CreateXm lDeclaration("1 .0", "utf-8", String.Empty)
objRootNode = objDoc.Document Element
objDoc.InsertBe fore(xmldecl, objRootNode)
objDoc.Save("C: \outputXML.xml" )
objDoc = Nothing
End Sub

Private Function checkChildMenuI tems(ByVal sampleNode As XmlNode) As Integer
Dim returnvalue As Boolean
Dim i As Integer
returnvalue = False
For i = 0 To sampleNode.Chil dNodes.Count - 1
If (sampleNode.Chi ldNodes(i).Name ) = "menuItem" Then
returnvalue = True
Exit For
End If
Next
checkChildMenuI tems = returnvalue
End Function
Private Function menuItemsArrayL ist(ByVal sampleNode As XmlNode) As
ArrayList
Dim returnvalue As New ArrayList
Dim i As Integer
For i = 0 To sampleNode.Chil dNodes.Count - 1
If (sampleNode.Chi ldNodes(i).Name ) = "menuItem" Then
returnvalue.Add (sampleNode.Chi ldNodes(i))
End If
Next
menuItemsArrayL ist = returnvalue
End Function
*************** *************** ***************
"Cerebrus" wrote:
Hi Dickster,
My point really is this...
When iterating through each node in an XMlNodeList how do you know
where you are in the document hierarchy, in order to do this kind of insert
My answer : We (or atleast I) don't.

I think you have posed a very profound question. I thought XPath can
select nodes, but I don't know if it can be used to
insert/delete/append nodes.

The XPath: descendant::men uItem[parent::menuIte m] will select all
"menuItem" elements in the document, which have a menuItem parent, as
you intended. Note that they may be at different levels of the
heirarchy. Inserting / Appending remains up to us. But as you correctly
realized, it seems quite a task to discover where the current
"menuItem" is in that heirarchy.
I had not thought of using the OuterXML property of XmlDocumentFrag ment
Cerebus -I am very interested to see how you do step 4/5/6.


Actually I have used the OuterXml property(ReadOn ly) of the "menuItem"
node and assigned it to the InnerXml property of the
XmlDocumentFrag ment.

For the steps, refer to my answer to Neeraj.

And lastly, it's Cerebrus...with an "r" ;-))

Regards,

Cerebrus.

Mar 24 '06 #12
Hi Neeraj,

I tried your code, and got a lot of errors. After correcting most of
them, I was able to generate the Output.xml file, and it does the job
for most of the MenuItems, except the last set within the HELP menu.

I do not think that a program will function differently, when run in
Debug mode and without Debugging. So, the problem must lie within the
logic. It runs the same for me with or without debugging.

Did you try the code I suggested ? Does it work or are you having
problems with that ?

Regards,

Cerebrus.

Mar 24 '06 #13
Hi Cerebrus (I'll get my spelling right from now on)

Apart from College I havent really used many recursive formulas so its
nice to see this one in action.

Its a good solution that answers alot of my questions & neerajb
question..
I'll post my question about position in the XDoc as a new thread see if
there is any response.

A code enhancement:
Probably move the 2nd recursive call to ParseChildren into the While
Loop - that way it handles imbedded menuitems in any of the siblings
(or at least it appeared to with the testing i did) - I stand to be
corrected

Private Sub ParseChildren(B yVal theNode As XmlNode)
Dim menuItemNode, subMenuNode As XmlNode

'Don't bother trying Text nodes.
If theNode.NodeTyp e = XmlNodeType.Ele ment Then

If theNode.HasChil dNodes = True Then
menuItemNode = theNode.SelectS ingleNode("./menuItem")

'Proceed only if any nested menuItem elements are
present.
If Not (menuItemNode Is Nothing) Then

'Add a new "subMenu" node
subMenuNode = xDoc.CreateElem ent("subMenu")
AddToSubMenu(me nuItemNode, theNode,
subMenuNode)
'Repeat for any children menuItems
ParseChildren(s ubMenuNode.Firs tChild)
'No more children menuItems are present for
this node,
'so continue with next sibling.
menuItemNode = subMenuNode.Nex tSibling

'Dickster: ParseChildren has been moved from
here into the While Loop
'immediatley below

While Not (menuItemNode Is Nothing)
ParseChildren(m enuItemNode)
AddToSubMenu(me nuItemNode, theNode,
subMenuNode)
menuItemNode = subMenuNode.Nex tSibling
End While

End If
End If
End If

Mar 24 '06 #14
Hi Dickster,

Thanks a lot, buddy, for that code enhancement. Actually I had tried to
put the ParseChildren statement into the While loop, exactly at the
position you did, but for some reason I got a Null reference exception
(again !).

Now, when I tried it again, it works !

I guess working on a programming problem at 4 am, *does* take it's toll
! ;-)
I'll post my question about position in the XDoc as a new thread see if
there is any response.


Yeah, I think that's the best way to get the experts on our problem...

Warm regards,

Cerebrus.

P.S.: I would like to keep up correspondence with you in the future, if
that's OK with you.

Mar 24 '06 #15

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

Similar topics

2
14885
by: john smith | last post by:
Hello, I am having trouble getting the parent node of an xml document using xslt. I am trying to return the node wine, but this does not return wine but the element values under wine. I thouht this would work. I am looking for the prices context and then ask for the parent node right? What am I doing wrong? Thanks in advance for your help.
2
1403
by: David Lei | last post by:
Hi, How do I add a new parent node to the existing nodes? I have an existing XmlDocument object like this: <item> <something>ABC</something> <item> <item> <something>DEF</something>
5
3809
by: rmlakshmanan | last post by:
In javascript a tree structure node i have a subnode and it has parentnode and that parentnode has one parent how do i get all parent node when i click the subnode
3
5459
by: Irocivan | last post by:
Hello, I download a nice collapse menu for free distribution from the internet. It works very well except that none of the parents nodes are clickable (i.e. when clicking on the parent node, the link does not work). For example, if the below content is included in the html page, I would like that page www.fruit.com can be loaded whenever I click it. However the javascript does not allow the page to be loaded, It would only work for the child...
2
2108
by: Rob Stevens | last post by:
When restoring nodes to a tree, how can you tell what is the parent of that node? Also how can you get a handle to the parent node, this way you can insert the node into the correct place? example. Parent1, Level0 Child1, Level1
0
1169
by: jalicej | last post by:
In my C# web application i have a treeview control with showcheckboxes = All . So for each node the check box is shown. When i check on the parent node all its child nodes should be checked automatically. similarlly when i uncheck a parent node all its childnodes should be unckecked. How can i do this?
0
986
by: Limno | last post by:
Hi, I have a treeview control with showcheckboxes = All in my C#.net web application. When i check on the child node its parent node should be checked automatically.similarlly when i uncheck a parent node all its childnodes should be unckecked. When i check on the parent node all its child nodes should be checked automatically. How can i do this?
4
3585
by: jmDesktop | last post by:
I have searched everywhere and tried several things. I have a treeview with and want to be able to only select a parent node. For example: root //don't want to drag this -parent1 //yes, drag this an only this because it is a parent --childOfParent1 //cannot drag this, only the parent --childOfParent1 //can only drag parent -parent2 //yes, parent, can select and drag --childOfParent2 //no, cannot drag
1
3681
by: SWETA123 | last post by:
I will very helpful to you if you can help me out in my below problem. I am not getting idea what should be the right approach for this. Here is my problem. I have below (sample)xml. <Rules> <MapRule input="@PARENT()" output="Sales_Totals" /> <MapRule input="''" output="Sales_Totals@name" /> <MapRule input="''" output="Sales_Totals@type" /> <MapRule input="" output="Sales_Totals@allTimeSales" />
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.