simple xml parse with xpath and populate a dropdown list

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.
LVL 12
jazzIIIloveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
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
 
Ken ButtersCommented:
Try two slashes to start :

"//Envelope/Body/Aresponse/Mys/My/primerId"
0
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Geert BormansConnect With a Mentor Information ArchitectCommented:
This article gives you some insigth in namespaces and how to handle them
http://support.microsoft.com/kb/318545
0
 
jazzIIIloveAuthor Commented:
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
 
Ken ButtersConnect With a Mentor Commented:
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
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
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
 
jazzIIIloveAuthor Commented:
<primerId>3</primerid> was a typo but my xml is a soap message actually which starts with Envelope.
0
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
Also make sure you use selectNodes, not selectSingleNode,
it seems you can have more than one result node
0
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
jazzIIIloveAuthor Commented:
Thanks guys. Working now. I felt the spirit of old EE here
0
 
jazzIIIloveAuthor Commented:
Geert. Extremely precise guessing
0
All Courses

From novice to tech pro — start learning today.