Solved

xpath xml insert data duplicate nodes

Posted on 2012-03-11
7
371 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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