Solved

simple xml parse with xpath and populate a dropdown list

Posted on 2014-03-13
13
504 Views
Last Modified: 2014-03-14
Hi there;

I have the following xml:
<Envelope>
<Body>
<Aresponse>
<Mys>
<My>
<primerId>3</primerid>
<secondaryId>5</secondaryId>
</My>
<My>
<primerId>4</primerid>
<secondaryId>6</secondaryId>
<Mys>
....

Open in new window


I need to do in XPath of C#.
I used this XPath but failed simply node is not found.

"/Envelope/Body/Aresponse/Mys/My/primerId"

Give a hand?

Regards.
0
Comment
Question by:jazzIIIlove
[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
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39927266
Try two slashes to start :

"//Envelope/Body/Aresponse/Mys/My/primerId"
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 358 total points
ID: 39927301
Two slashes or one does not make a difference when Envelope is the root

I see that the XML is not wellformed, so I think you cut down from a larger example
and dropped the attributes and namespace nodes
Can you show the real example, since your XPath is depending on the namespace information

If you did not cut down from a bigger example, than the XML simply does not load correctly
<primerId>3</primerid>
Case is important, so you need to close the element with a uppercase I in the name
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 358 total points
ID: 39927318
A simple test would be this XPath

/*[local-name() ='Envelope']/*[local-name() ='Body']/*[local-name() ='Aresponse']/*[local-name() ='Mys']/*[local-name() ='My']/*[local-name() ='primerId']

It makes each element node namespace independent

If that XPath works, it definitely is a namespace issue
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 358 total points
ID: 39927325
This article gives you some insigth in namespaces and how to handle them
http://support.microsoft.com/kb/318545
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39927703
Hi,

Envelope is the root and my xml content doesn't start with <?xml version='1.0'?>

This is the simplied version of my xml. No way with XPath?

Regards.
0
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 71 total points
ID: 39927746
It is critical that the end tags match your start tags.

you have a capital "I" in the start tag, and a lowercase 'i' in the end tag.

<primerId>3</primerid>

Try changing that and see if that helps.
You have that in both occurrences of primerId.

It looks like the last Msys should be an ending tag... but it is not.... that is hard to know without seeing your entire xml file.

Here is an updated XML that may work better as a test:

<?xml version='1.0'?>
<Envelope>
  <Body>
    <Aresponse>
      <Mys>
        <My>
          <primerId>3</primerId>
          <secondaryId>5</secondaryId>
        </My>
        <My>
          <primerId>4</primerId>
          <secondaryId>6</secondaryId>
        </My>
      </Mys>
    </Aresponse>
  </Body>
</Envelope>

Open in new window

0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 358 total points
ID: 39927893
Hi JazzIIILove,

My point exactly is that if this is a simplified version of your XML, you are likely filtering out namespace nodes

<Envelope>
and
<Envelope xmlns="http://www.w3.org/2001/12/soap-envelope">
Are simply two completely different things
"/Envelope"
will only catch the first
"/*[local-name() = 'Envelope']"
will catch both, but is clumsy

I sent you a full XPath, you should test.
If that XPath (the thing with a number of local-name tests in it) works on your data then your issue is the namespace

I could see it was simplified, because it is not wellformed. I pointed that out and Ken just sent a corrected version of it.

Testing your code on Ken's example might hide the real issue. It is obvious that your original XPath will do the right thing on that one
As I said in my first message, the problem could be two-fold
1) simplification changed the context because of stripping out namespace nodes
2) there is an error in the file (there are multiple errors in your snippet)

In order for us to validate which of the two is the issue, post the entire XML
or test the Xpath I gave in my second or third message
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39927958
<primerId>3</primerid> was a typo but my xml is a soap message actually which starts with Envelope.
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 358 total points
ID: 39927966
Are you reading all the messages?

My first message already implied it was a namespace issue
A Soap envelope can have a default namespace, I implied in my previous message that the Envelope was SOAP (check the example)

please try this XPath,( first posted four hours ago)

/*[local-name() ='Envelope']/*[local-name() ='Body']/*[local-name() ='Aresponse']/*[local-name() ='Mys']/*[local-name() ='My']/*[local-name() ='primerId']

And let us know the outcome
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39928100
Also make sure you use selectNodes, not selectSingleNode,
it seems you can have more than one result node
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 71 total points
ID: 39928332
I agree with the others:  Start with well-formed XML...the .NET classes expect nothing less.

Outside of that, if your XML has any namespaces (which you are not showing), then you also need to use an XmlNamespaceManager when you are doing the selecting.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39930160
Thanks guys. Working now. I felt the spirit of old EE here
0
 
LVL 12

Author Closing Comment

by:jazzIIIlove
ID: 39930170
Geert. Extremely precise guessing
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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