473,761 Members | 2,384 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 #1
14 11505
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 22 '06 #2
I'm having trouble getting an Xpath Expression to pick out the relevant
nodes

e.g.
descendant::men uItem[parent::menuIte m][not(preceding-sibling::menuIt em)]

Cerebus - I Would like to see to insert a new parent node around the
relevant menuItem node after having got the relevant nodes in a
nodelist

i.e.

xdoc.Load("xpat h.xml")

Dim nl As XmlNodeList = xdoc.DocumentEl ement. _
SelectNodes("de scendant::menuI tem[parent::menuIte m][not(preceding-sibling::menuIt em)]")

For Each xnode In nl

txEle = xdoc.CreateElem ent("submenu")
txEle.AppendChi ld(xnode)

'Now how do we update xdoc
'replacing xnode with txEle
' because at this point in code we dont know where we
are in xdoc
Next
Dickster

Mar 23 '06 #3
Hi Dickster,

Is your question an answer to Neeraj's question ? Or is your question
separate from Neeraj's original question ?

The way I was thinking of solving Neeraj's problem was : (xDoc is the
XmlDocument)

1. Get a NodeList containing all Top level "menuItem" nodes, using
xDoc.SelectNode s("/menu/menuItem")
2. Iterate through this list, calling a method, say "ParseChildren( )"
3. The ParseChildren method will recursively process each child
"menuItem" node, using SelectSingleNod e(".//menuItem")
4. During processing, it creates an XmlDocumentFrag ment containing all
the OuterXml of the "menuItem" node.
5. It then creates a "subMenu" element just before this "menuItem"
node. Then the XmlDocumentFrag ment is appended as a child to this
"subMenu" element.
6. The original "menuItem" node is then deleted.
7. The loop moves on to the next sibling "menuItem" node, if no sibling
exists, it moves to the parent's next sibling.

I've got some of the code to work (uptill step 6), but I'm still stuck
at iterating through the siblings. Hopefully I'll find some time to
debug my code soon.

The XPath expression you presented selects only the first nested
"menuItem" element, since you have added the
not(preceding-sibling::menuIt em) part.

HTH,

Regards,

Cerebrus.

Mar 23 '06 #4
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 23 '06 #5
Hi dickster,

Thanks for the reply.

Your Xpath is not giving the required nodes. it should give menuItem nodes
which are having menuItem Childs so that we can enclose them in subMenu tags.

like in case of input xml it should give REPORTS,Standar d Reports, Custom
Reports and HELP, so that we can include their menuitem tags in submenu tag.

"dickster" wrote:
I'm having trouble getting an Xpath Expression to pick out the relevant
nodes

e.g.
descendant::men uItem[parent::menuIte m][not(preceding-sibling::menuIt em)]

Cerebus - I Would like to see to insert a new parent node around the
relevant menuItem node after having got the relevant nodes in a
nodelist

i.e.

xdoc.Load("xpat h.xml")

Dim nl As XmlNodeList = xdoc.DocumentEl ement. _
SelectNodes("de scendant::menuI tem[parent::menuIte m][not(preceding-sibling::menuIt em)]")

For Each xnode In nl

txEle = xdoc.CreateElem ent("submenu")
txEle.AppendChi ld(xnode)

'Now how do we update xdoc
'replacing xnode with txEle
' because at this point in code we dont know where we
are in xdoc
Next
Dickster

Mar 23 '06 #6
Hi Cerebus, - thanks for your reply.

Neerajb - thanks for your reply & sorry for hopping in on the back of
your query.

I'm referring to Neeraj problem as it is an example of problem I would
like to see solved.

i.e. how to

1. take a list of child nodes (XmlNodeList) which match a particular
Xpath expression but are dotted randomly throughout an XML hierarchy
2. Imbed each of those child nodes in a parent node (eg. <subItem>)
back into the main XML message

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.

Indeed my XPath was wrong - but I would be interested to see an XPath
Expression which would pick out the exact list of nodes Neeraj needs
imbedded in a new <subItem> node. - I'm not sure if this is
possible.

Dickster

Mar 23 '06 #7
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

Mar 23 '06 #8
Hi Neeraj,

After banging my head for quite a while, I've got it to work. It turns
out that the code isn't as beautiful as I would've liked, but frankly I
was fed up of Null Reference exceptions ~!!

Points to note :
---------------------
1. Copy and paste it into a Windows Form .vb file, and run Edit ->
Advanced -> Format Document. Don't try to view it here, since I have
removed all Tabs.
2. Add a Button named Button1 to your Form. This is needed only because
I start the processing at the click of the button.
3. I haven't used your code, since I had already progressed to some
extent with my own code, before your second post.
4. Hopefully, you can improve the code. I do hate repetitive code
statements, but in this case, it seemed necessary.
5. I have tried to comment inline, as much as possible, to allow you to
follow the program flow.
6. I have tested it with upto 5 levels of "menuItem" nesting.

=============== =============== ==
Imports System.Xml
Imports System.Xml.XPat h
:
:
Dim xDoc As XmlDocument
Dim xNode As XmlNode
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
xDoc = New XmlDocument()
xDoc.Load("Inpu t.xml")
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim xList As XmlNodeList = xDoc.SelectNode s("/menu/menuItem")
Try
For Each xNode In xList
ParseChildren(x Node)
Next
xDoc.Save("Outp ut.xml")
MsgBox("Alright y, then...")
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
End Sub

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
'Parse for any child menuItems of this sibling
ParseChildren(m enuItemNode)

While Not (menuItemNode Is Nothing)
AddToSubMenu(me nuItemNode, theNode, subMenuNode)
menuItemNode = subMenuNode.Nex tSibling
End While
End If
End If
End If
End Sub

Private Sub AddToSubMenu(By Val menuItemNode As XmlNode, ByVal
parentNode As XmlNode, ByVal SubMenuNode As XmlNode)
'Store the contents of this "menuItem" node in a DocFragment, including
the menuItem outer xml.
Dim frag As XmlDocumentFrag ment = xDoc.CreateDocu mentFragment()
frag.InnerXml = menuItemNode.Ou terXml
'Append contents of Frag.
SubMenuNode.App endChild(frag)
parentNode.Inse rtBefore(SubMen uNode, menuItemNode)
parentNode.Remo veChild(menuIte mNode)
End Sub
=============== =============== ==
Hope this helps,

Regards,

Cerebrus.

Mar 23 '06 #9
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 23 '06 #10

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
3808
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
5458
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
2107
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
9522
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
9336
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
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
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
6603
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();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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

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.