Solved

simple xml parse with xpath and populate a dropdown list

Posted on 2014-03-13
13
494 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 358 total points
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
Also make sure you use selectNodes, not selectSingleNode,
it seems you can have more than one result node
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 71 total points
Comment Utility
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
Comment Utility
Thanks guys. Working now. I felt the spirit of old EE here
0
 
LVL 12

Author Closing Comment

by:jazzIIIlove
Comment Utility
Geert. Extremely precise guessing
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

9 Experts available now in Live!

Get 1:1 Help Now