Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

xpath xml insert data duplicate nodes

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
goodluck11
Asked:
goodluck11
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
What do you mean by "duplicate nodes"? It's not clear from your example XML what you mean.
0
 
anarki_jimbelCommented:
Yes, please explain the task
0
 
goodluck11Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
käµfm³d 👽Commented:
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
 
anarki_jimbelCommented:
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
 
käµfm³d 👽Commented:
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
 
anarki_jimbelCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now