How to find a node in XML
I have the code below that looks at an XML file to find a node named IssueMaterialPO and finds it successfully in the XML listed in the second code block. It doesn't find anything in the XML in the third code block. I have tried various search strings in the SelectSingleNode argument, all with no luck. I'm not sure if my search is wrong, if it's something to do with the namespace, or something else entirely. I'm new to working with XML and am not sure what to try next.
Code:
Try
Dim doc As New XmlDocument
doc.Load(myXMLfile)
Dim nsMgr As New XmlNamespaceManager(doc.NameTable)
nsMgr.AddNamespace( "ext_UserSchema", "http://Epicor.com/SC/UserSchema/" )
nsMgr.AddNamespace( "msg", "http://Epicor.com/Message/2.0" )
Dim nodeToFind As XmlNode
Dim root As XmlElement = doc.DocumentElement
nodeToFind = root.SelectSingleNode( "//IssueMaterialPO", nsMgr)
If (nodeToFind Is Nothing) Then
MsgBox( "Not Found")
Else
IMPO = True
MsgBox( "Found")
End If
Finds IssueMaterialPO in this block
Code:
<?xml version="1.0" encoding="ISO8859-1" ?>
<BatchRecord>
<RecordNumber>11340</RecordNumber>
<Date>2012-03-09</Date>
<ERP>
<Transactions>
<IssueMaterialPO>
<Bin>BIN</Bin>
<OrderNumber>555555</OrderNumber>
<InventoryCategory>O</InventoryCategory>
<LotNumber>678</LotNumber>
<ItemNumber>12-345</ItemNumber>
<Quantity>10</Quantity>
<Stockroom>01</Stockroom>
</IssueMaterialPO>
</Transactions>
</ERP>
</BatchRecord>
Doesn't find IssueMaterialPO in this block
Code:
<?xml version="1.0" encoding="utf-16"?>
<msg:Msg xsi:schemaLocation="http://Epicor.com/Message/2.0 http://scshost/schemas/epicor/ScalaMessage.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://Epicor.com/Message/2.0">
<msg:Hdr>
<msg:Ctrl>
<msg:MsgID></msg:MsgID>
</msg:Ctrl>
<msg:Sender>
<msg:Name></msg:Name>
<msg:Subname></msg:Subname>
</msg:Sender>
<msg:Logon></msg:Logon>
</msg:Hdr>
<msg:Body>
<msg:Req msg-type="transaction" action="ConvertXML">
<msg:Dta>
<ext_UserSchema:BatchRecord xmlns:msg="http://Epicor.com/InternalMessage/1.1" xmlns:ext_UserSchema="http://Epicor.com/SC/UserSchema">
<ext_UserSchema:RecordNumber>11340</ext_UserSchema:RecordNumber>
<ext_UserSchema:Date>2012-03-09</ext_UserSchema:Date>
<ext_UserSchema:ERP>
<ext_UserSchema:Transactions>
<ext_UserSchema:IssueMaterialPO>
<ext_UserSchema:Bin>BIN</ext_UserSchema:Bin>
<ext_UserSchema:OrderNumber>555555</ext_UserSchema:OrderNumber>
<ext_UserSchema:InventoryCategory>O</ext_UserSchema:InventoryCategory>
<ext_UserSchema:LotNumber>678</ext_UserSchema:LotNumber>
<ext_UserSchema:ItemNumber>12-345</ext_UserSchema:ItemNumber>
<ext_UserSchema:Quantity>10</ext_UserSchema:Quantity>
<ext_UserSchema:Stockroom>01</ext_UserSchema:Stockroom>
</ext_UserSchema:IssueMaterialPO>
</ext_UserSchema:Transactions>
</ext_UserSchema:ERP>
</ext_UserSchema:BatchRecord>
</msg:Dta>
</msg:Req>
</msg:Body>
</msg:Msg>
Re: How to find a node in XML
Have a look here: http://msdn.microsoft.com/en-us/library/h0hw012b.aspx
Basically it'll boil down to you needing to provide the namespace in your SelectSingleNode's XPath expression.
Re: How to find a node in XML
I've tried this, but it didn't find it. Where did I go wrong?
Code:
nodeToFind = root.SelectSingleNode("/msg:Msg/msg:Body/msg:Req/msg:Dta/ext_UserSchema:BatchRecord/ext_UserSchema:ERP/ext_UserSchema:Transactions/ext_UserSchema:IssueMaterialPO", nsMgr)