Get Multiple Elements on the same Leaf

Hello,

I have some XML
<exhibitor>
                     <primary_key/>
                     <company_name></company_name>
                     <state></state>
                     <zip></zip>
                     <country_code></country_code>
                     <contact>
                        <first_name></first_name>
                        <last_name></last_name>
                        <credentials/>
                        <phone/>
                        <phone_ext/>
                     </contact>
                  </exhibitor>
I Am trying to using XPath get the primary key and contact elements while keeping them in the exhibitor root node?

XmlNodeList ExhibitorNodes2 = ExhibitorsNode2.SelectNodes("exhibitor/*[self::key_id or self::contact]");

this is just separating them and giving me the key_id and contact as a list i want them to stay in their exhibitor root?
Kevin CabralSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
I do not really understand what you want to achieve?

If you have a key_id element (primary_key ?) and contacts elements you my navigate to a root from them.

If you want some new structure you need to copy elements and link them.
0
Kevin CabralSoftware DeveloperAuthor Commented:
As it is now this XMLNodeList will return me primary_key, contact, primary_key, contact; I would like to rip it while keeping it in its top level exhibitor, that way the XMLNodeList is a list of exhibitors with the information inside.
0
Fernando SotoRetiredCommented:
You have shown us what the XML document looks like before, can you now show us what you want the result of manipulation to look like.

Also does it need to be using XPath and XmlDocument objects or are you open to Linq to XML?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Kevin CabralSoftware DeveloperAuthor Commented:
<exhibitor>
                     <primary_key/>
                        <first_name></first_name>
                        <last_name></last_name>
                        <credentials/>
                        <phone/>
                        <phone_ext/>
                  </exhibitor>

this is my end goal with what was in contact being essentially out, I'm open to anything although I am working in a .net environment at this point so  as long as i can pull of what i need to there i'm good.

Once i have something in the node list i'm using a set of libraries we have here internally at my place to Fill Data From XML to a dictionary then load it into SQL.
0
anarki_jimbelSenior DeveloperCommented:
Basically, you need to construct a new XML document from the list you get from the original document.

Something like below:

1. Create a new xml document
2. Attach a root
3. create a new <exhibitor> element and add it to the document (root)
4. add <primary_key/> and other elements to the <exhibitor>
5. may convert back to text form if you need.
0
Fernando SotoRetiredCommented:
Hi Kevin;

The following code snippet should do what you need. I assumed that there will be only one set of nodes in the root node exhibitor. If this is not the case please let me know.
using System.Xml.Linq;

// Load the XML from the file system.
var xdoc = XDocument.Load("Place path to the XML document here");
// Use this line in place of the above line if XML is in string
// format in a variable in memory
var xdoc = XDocument.Parse(xmlVariable);
// Get the XML elements in document order
var nodes = from n in xdoc.Root.Elements()
            select n;
// Create a new XML document with the root element exhibitor                      
XDocument newDoc = new XDocument(new XElement("exhibitor"));
// Add the primary_key element to the new document
newDoc.Root.Add(nodes.Where( n => n.Name == "primary_key").First());
// Iterate the collection of child elements of contact add to new document
foreach (XElement n in nodes.Elements())
{
    newDoc.Root.Add(n);
}

// To save the new document to the file system
newDoc.Save("File path go here");
// To use the XML as a string use
string strNewDoc = newDoc.ToString();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.