XML Functions problem

  • Thread starter Thread starter Bryan Dickerson
  • Start date Start date
B

Bryan Dickerson

I know that this structure is accurate, so I'm trying a test to see if what
I get back from this:

Dim xElem As Xml.XmlElement
Dim sVal As String
xElem = xmlPO.GetElementById("Body/PurchaseOrder/OrderHeader")
sVal = xElem.GetAttribute("BuyerOrderNumber")

Does anyone see an obvious problem, if, as I said, the XML hierarchy is
correct?
 
Possibly:

xElem = xmlPO.GetElementById("/Body/PurchaseOrder/OrderHeader")
sVal = xElem.GetAttribute("@BuyerOrderNumber")
 
I just stepped thru it and watched the "Autos" box a bit more closely and
xElem is still set to "Nothing" after the first statement below and gets
"Object reference not set to an instance of the object" as an error message.
I tried "Dim"-ming xElem to a New XML.XmlElement, but the build didn't work
and said "Overload resolution failed because no 'New' is accessible."

Hmmm...

Any ideas?
 
Bryan said:
I just stepped thru it and watched the "Autos" box a bit more closely and
xElem is still set to "Nothing" after the first statement below and gets
"Object reference not set to an instance of the object" as an error message.
I tried "Dim"-ming xElem to a New XML.XmlElement, but the build didn't work
and said "Overload resolution failed because no 'New' is accessible."

Hmmm...

Any ideas?

xElem = xmlPO.GetElementById("/Body/PurchaseOrder/OrderHeader")
if xElem is nothing then
messagebox.show("You did not find any elements using that xpaht")
else
sVal = xElem.GetAttribute("@BuyerOrderNumber")
end if

You can't new the xelem, it has to be returned by the GetElementById or
a similar function.

Chris
 
So that would mean that the GetElementByID function didn't work, right?
Here's a snippet of the XML Doc:

-------------------------------------------------------------------
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<PurchaseOrder>
<OrderHeader>
<BuyerOrderNumber>TestPo1</BuyerOrderNumber>
 
Bryan said:
So that would mean that the GetElementByID function didn't work, right?
Here's a snippet of the XML Doc:

-------------------------------------------------------------------
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<PurchaseOrder>
<OrderHeader>
<BuyerOrderNumber>TestPo1</BuyerOrderNumber>

Yes that it what it means... I've used xpath very little but if you
take out the "SOAP-ENV:" in front of body, does it work?

chris
 
If I take out the SOAP-ENV, the functions to load the XML document don't
work.
 
Bryan,

You don't want to remove the namespace from the node. What you need to
do is inform the parser of the namespace. You would think that it
would be smart enough to handle this on its own...

What you need to do is define the namespace in an XmlNamespaceManager
instance. Here is a sample:

http://samples.gotdotnet.com/quickstart/howto/doc/Xml/XmlNameSpace.aspx

Once you set up the XmlNamespaceManager, you should be able to access
the element with:
//SOAP-ENV:Body/PurchaseOrder/OrderHeader

Let me know if that works for you. HTH!

Joseph
 
Does it make any difference that there isn't the "<?xml version="1.0"> at
the beginning of the document??
 
My reply from yesterday afternoon, was posted before I read this note. I
think this will help, but I need some more time to digest it. I'll let you
know, though. Thanx for the help!
 
Not a problem, Bryan. Just post back here if it doesn't. I am pretty
sure that will take care of your problem. If not, I'll look into it a
little more.

Joseph
 
Ok, this is the code that I've come up with:

xTest.LoadXml(sData)
xNSM = New Xml.XmlNamespaceManager(xTest.NameTable)
xNSM.AddNamespace("SOAP-ENV", "http://tempuri.org/SOAP-ENV")

xElem =
xTest.GetElementById("//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerOrderNumber")
sVal = xElem.Value

But I'm still getting the error: "Object reference not set to an instance of
an object." on the last line of code that I included here. I'm still not
getting anything returned from the GetElementById function.

What now?
 
Bryan,

You are almost there... ;-) Inline.

Bryan said:
xTest.LoadXml(sData)
xNSM = New Xml.XmlNamespaceManager(xTest.NameTable)
xNSM.AddNamespace("SOAP-ENV", "http://tempuri.org/SOAP-ENV")

Here is your first problem. When you add the namespace, it needs to
match the namespace declaration in the XML. It uses it is a lookup, so
if both parts don't match, it fails. Change this line in your code to:

xNSM.AddNamespace("SOAP-ENV",
"http://schemas.xmlsoap.org/soap/envelope/")
xElem =
xTest.GetElementById("//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerOrderNumber")
sVal = xElem.Value

The problem here is that you need to query within the context of the
XmlNamespaceManager. To do this, use SelectSingleNode(), like this:

Dim nodRetrieve As XmlNode
nodRetrieve =
xTest.SelectSingleNode("//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerOrderNumber",
xNSM)
But I'm still getting the error: "Object reference not set to an instance of
an object." on the last line of code that I included here. I'm still not
getting anything returned from the GetElementById function.

What now?

So, I made a quick and dirty sample using the snippet of the XML that
you made along with a modified version of the source you posted. Mine
looks like this:

Dim xTest As New XmlDocument
Dim sData As String = _
"<SOAP-ENV:Envelope
xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
"<SOAP-ENV:Body>" & _
"<PurchaseOrder>" & _
"<OrderHeader> " & _
"<BuyerOrderNumber>TestPo1</BuyerOrderNumber> " & _
"</OrderHeader> " & _
"</PurchaseOrder>" & _
"</SOAP-ENV:Body>" & _
"</SOAP-ENV:Envelope>"

Dim xNSM As System.Xml.XmlNamespaceManager
Dim xElem As XmlElement
Dim sVal As String

xTest.LoadXml(sData)
xNSM = New Xml.XmlNamespaceManager(xTest.NameTable)
xNSM.AddNamespace("SOAP-ENV",
"http://schemas.xmlsoap.org/soap/envelope/")

Dim nodRetrieve As XmlNode
nodRetrieve =
xTest.SelectSingleNode("//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerOrderNumber",
xNSM)
sVal = nodRetrieve.InnerText

Let me know if that gets it working for you!

Joseph
 
You're a genius!! "Thank You" * 1000 !! That finally gets the log-jam
cleared. Now, I just need to go back and "get to know" the code a bit
better. Nonetheless, thanx again!
 
Next question: How do I reference this line?

<BuyerReferences RefType="DepartmentNumber">951</BuyerReferences>

TIA!
 
I am not sure what the full XPath is, since I don't know your full body
structure. However, I am assuming that you are wondering how to find a
node for a specific attribute value, correct?

If so, your XPath would be something like:
//SOAP-ENV:Body/PurchaseOrder/BuyerReferences[@RefType='951']

Note, that I just assumed that was where your node would be. You will
have to adjust that XPath based upon your structure.

Joseph
 
This is the xml snippet:

-------------------------------------------------------------------
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<PurchaseOrder>
<OrderHeader>
<BuyerOrderNumber>TestPo1</BuyerOrderNumber>
<BuyerReferences
RefType="DepartmentNumber">951</BuyerReferences>
<BuyerReferences
RefType="InternalVendorNumber">123324</BuyerReferences>
 
Bryan said:
This is the xml snippet:

-------------------------------------------------------------------
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<PurchaseOrder>
<OrderHeader>
<BuyerOrderNumber>TestPo1</BuyerOrderNumber>
<BuyerReferences
RefType="DepartmentNumber">951</BuyerReferences>
<BuyerReferences
RefType="InternalVendorNumber">123324</BuyerReferences>

To grab "951", change the XPath from the sample I had posted to:

//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerReferences[@RefType='DepartmentNumber']

To get all of the BuyerReferences, it is a little different. You are
selecting multiple nodes, so you can get them all by changing the
SelectSingleNode() to SelectNodes(). From there, you can iterate
through them. Like this:

Dim ndlBuyerReferences As XmlNodeList
Dim nodBuyerReference As XmlNode

ndlBuyerReferences =
xTest.SelectNodes("//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerReferences",

xNSM)

For Each nodBuyerReference In ndlBuyerReferences

'// Your individual nodBuyerReference is here.

Next

Joseph
 
Thank you again! Now if I can beg the answer to one more question: Using
my snippet as an example, I created the NodeList and looped thru the Nodes
successfully, but where is the "RefType" value hidden in the Node?

TIA!

Joseph Ferris said:
Bryan said:
This is the xml snippet:

-------------------------------------------------------------------
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<PurchaseOrder>
<OrderHeader>
<BuyerOrderNumber>TestPo1</BuyerOrderNumber>
<BuyerReferences
RefType="DepartmentNumber">951</BuyerReferences>
<BuyerReferences
RefType="InternalVendorNumber">123324</BuyerReferences>

To grab "951", change the XPath from the sample I had posted to:

//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerReferences[@RefType='DepartmentNumber']

To get all of the BuyerReferences, it is a little different. You are
selecting multiple nodes, so you can get them all by changing the
SelectSingleNode() to SelectNodes(). From there, you can iterate
through them. Like this:

Dim ndlBuyerReferences As XmlNodeList
Dim nodBuyerReference As XmlNode

ndlBuyerReferences =
xTest.SelectNodes("//SOAP-ENV:Body/PurchaseOrder/OrderHeader/BuyerReferences",

xNSM)

For Each nodBuyerReference In ndlBuyerReferences

'// Your individual nodBuyerReference is here.

Next

Joseph
 
Bryan,

Not a problem at all...

"RefType" is an attribute to a node in your document. You can find it
in the Attributes collection of the XmlNode, which is an
XmlAttributeCollection object.

To iterate through them, you can use a similar For Each construct.

For Each nodBuyerReference In ndlBuyerReferences

'// Your node is here in nodBuyerReference.

Dim attBuyerReference As XmlAttribute

For Each attBuyerReference In nodBuyerReference.Attributes

'// Your attribute is here, in attBuyerReference.

Next

Next

HTH!

Joseph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top