Solved

xpath xml insert data duplicate nodes

Posted on 2012-03-11
7
363 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 74

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

15 Experts available now in Live!

Get 1:1 Help Now