473,396 Members | 1,938 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,396 software developers and data experts.

navigate to correct XML node ??

<root>
<person>
<name>bob</name>
<age>30</age>
</person>
<person>
<name>bill</name>
</person>
</root>

I'd like to navigate to the LAST "person" node and insert an "age"
element.

if I use*:
XmlNode insertNode = xmlDoc.SelectSingleNode("/root/person");
and
XmlElement xe = xmlDoc.CreateElement("age");
xe.innerText = "40";
insertNode.AppendChild(xe);
, a new "age element is inserted in the FIRST "person" node.

*not the complete code, but the main idea

How can I insert my "age" element in the LAST "person" node of the
xmlDoc?

Thanks much, Tim :)

Jun 11 '07 #1
6 2087
On Jun 11, 10:22 am, "Timothy W. Molter" <tim.mol...@gmail.comwrote:
<root>
<person>
<name>bob</name>
<age>30</age>
</person>
<person>
<name>bill</name>
</person>
</root>

I'd like to navigate to the LAST "person" node and insert an "age"
element.

if I use*:
XmlNode insertNode = xmlDoc.SelectSingleNode("/root/person");
and
XmlElement xe = xmlDoc.CreateElement("age");
xe.innerText = "40";
insertNode.AppendChild(xe);
, a new "age element is inserted in the FIRST "person" node.

*not the complete code, but the main idea

How can I insert my "age" element in the LAST "person" node of the
xmlDoc?

Thanks much, Tim :)
Can you use XmlNode.LastChild property to get the last person node
among childnodes?

Jun 11 '07 #2
XmlNode node = xmlDoc.DocumentElement.LastChild;
node.AppendChild(xmlDoc.CreateElement("age"));

HTH :)

Jun 11 '07 #3
thanks for the reply!

Yes, I could use that, but it's not exactly going to work for me.
After I posted. I realized you might post that solution, and I should
have stated my problem differently. If I have this XML:

<root>
<person>
<name>bob</name>
<age>30</age>
</person>
<person>
<name>bill</name>
</person>
<bike>
<brand>Trek</brand>
<color>blue</color>
</bike>
</root>

How can I insert an "age" element in the LAST "person" node of the
xmlDoc?

I hope you can answer that!

Thanks again, Tim

Jun 11 '07 #4
On Jun 11, 8:32 pm, "Timothy W. Molter" <tim.mol...@gmail.comwrote:
thanks for the reply!

Yes, I could use that, but it's not exactly going to work for me.
After I posted. I realized you might post that solution, and I should
have stated my problem differently. If I have this XML:

<root>
<person>
<name>bob</name>
<age>30</age>
</person>
<person>
<name>bill</name>
</person>
<bike>
<brand>Trek</brand>
<color>blue</color>
</bike>
</root>

How can I insert an "age" element in the LAST "person" node of the
xmlDoc?

I hope you can answer that!

Thanks again, Tim
Use XmlDataDocument.SelectNodes(string xPath) method providing path
for person node - "/root/person"

Jun 11 '07 #5
Nevermind, I found it! Here's how if anyone cares:

XmlNode insertNode = xmlDoc.SelectSingleNode("/root/person[last()]");

More good tricks at:

http://www.codeproject.com/soap/myXPath.asp

Jun 12 '07 #6
On Jun 12, 6:37 am, "Timothy W. Molter" <tim.mol...@gmail.comwrote:
Nevermind, I found it! Here's how if anyone cares:

XmlNode insertNode = xmlDoc.SelectSingleNode("/root/person[last()]");

More good tricks at:

http://www.codeproject.com/soap/myXPath.asp
Great!. Didn't know that last searchkey was there in XPath. nice to
know. Thanks.

Jun 12 '07 #7

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

Similar topics

3
by: guy | last post by:
Hi all - And apologies if I'm in the wrong newsgroup. I'm a newbie here and I'd certainly appreciate any pointers to the right forum. To the point... We're running a WebBrowser object...
8
by: yinjennytam | last post by:
Hi all, I'm new to .NET and XML and I have a question. Given an XML file, I want to navigate its content and look for one or two particular elements to get their values. At this point, it...
4
by: Greg | last post by:
How do I programmatically load an .aspx page into the contentplaceholder? Similar to what would happen in the "navigateURL" property when clicking on a hyperlink on the master page. . . ASP .NET...
1
by: =?Utf-8?B?TmFjaA==?= | last post by:
For Each tn As TreeNode In TreeView1.SelectedNode.Nodes *** If tn.ForeColor = Color.Blue Then *** Does not like above line. Can you give the correct syntax ??? I = I + 1 End If Next tn...
5
by: timnels | last post by:
I have a web browser control that I'd like to point at a HTML file in my installation directory. I am doing something like: string path = Path.GetDirectoryName(...
2
by: king imran | last post by:
////////////////////////////// LinkList.cpp #include "LinkList.h" /* The LinkList class implementation*/ /* Constructor */ LinkList::LinkList()
1
by: Falcula | last post by:
Hello, I have a treeview control, when i select a item i navigate to url. But selected node is lost, it reset itself, loosing state. I post my code here. Thanks in advance. <script...
4
by: Akshay Loke | last post by:
Hi all, I have this function from a class MFnDagNode, addChild( MObject & child, unsigned int index = kNextPos, bool keepExistingParents = false ); which takes an object reference as first...
1
by: DeveloperX | last post by:
Hi, I should probably start with my XML as it makes it easier to explain the problem. <?xml version="1.0" encoding="utf-8" ?> <aa note="top level"> <b1 note="b1"> <c1 note="b1 c1"> <d1...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
0
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...
0
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,...

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.