Solved

simple xml parse with xpath and populate a dropdown list

Posted on 2014-03-13
13
499 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
  • 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
 
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
3 Use Cases for Connected Systems

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

 
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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linq Help 1 35
XML XSLT If loop 4 21
C# parent child form 5 18
Code works but it's slow 24 40
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now