Solved

xpath xml insert data duplicate nodes

Posted on 2012-03-11
7
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 30

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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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 30

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 30

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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