Solved

xpath xml insert data duplicate nodes

Posted on 2012-03-11
7
365 Views
Last Modified: 2012-03-12
how do you insert data from c# into an xml that was duplicate nodes?

<books>
<bookdetails>
<name>123</name>
</bookdetails>
<bookdetails>
<name>345</name>
</bookdetails>
</books>
0
Comment
Question by:goodluck11
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37707931
What do you mean by "duplicate nodes"? It's not clear from your example XML what you mean.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 37707950
Yes, please explain the task
0
 

Author Comment

by:goodluck11
ID: 37710905
the //name node is the same repeated on the document

if we are using

XmlElement name= (XmlElement)doc1.SelectSingleNode("//name");

and have multiple nodes with <name>, how do we access node#1, node #2, and so on,,
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37710942
If you use SelectNodes instead of SelectSingleNode, then you would have access to all name nodes within the document (because you are applying the XPath to the document itself and not any particular child node). The XPath you are using would not need to change; just the function you are calling.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 37711638
I wrote a simple code:
        private void button1_Click(object sender, EventArgs e)
        {
         // read all book elements   
            XmlDocument doc = new XmlDocument();
            doc.Load(@"books.xml");
            XmlNodeList bookdetails = doc.GetElementsByTagName("bookdetails");

            // For each book oget a name and print it; any other book elements can be accessed here
            foreach (XmlNode book in bookdetails)
            {
                XmlNode nameNode = book.SelectSingleNode("./name"); // we suggest only one name element exists for a book
                System.Diagnostics.Debug.WriteLine("Name = " + nameNode.InnerText); // see output window
            }
        }

Open in new window


There are comments in the code, should be quitye clear.

I believe it is better to get book nodes ("bookdetails") and check any children like "name" elements, because books may have other elements like "author", ISBN", "publisher" etc.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37712044
I believe it is better to get book nodes ("bookdetails") and check any children like "name" elements, because books may have other elements like "author", ISBN", "publisher" etc.
If that's the case, then you can just alter the original XPath:

XmlNodeList names = doc1.SelectNodes("//bookdetails/name");

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 37712201
kaufmed, I agree, if we do care about names only - this is a good approach.
But what if I want to print names along with authors and ISBNs etc.? Get three or more not really synchronised lists?

Yes, of course, in my code to get books we may use XPath instead a GetElementsByTagName method :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now