alisman
asked on
selectsinglenode problem parsing xml in VB.net
Dim path = "......"
Dim xmldoc As New XmlDocument
Try
xmldoc.Load(path)
makeNewDataSet(xmldoc.Docu mentElemen t("results et"))
Catch ex As Exception
End Try
Then, in the subroutine "makenewdataset", I try to access values use selectsinglenode to grab field nodes by attribute "name"
n is a node representing a record.
Dim xpath = "/field[@name='first']"
Response.Write("--" & n.SelectSingleNode(xpath). InnerText)
This for some reason just shuts down the code. It doesn't throw an error. It just mysteriously breaks a loop.
Dim xmldoc As New XmlDocument
Try
xmldoc.Load(path)
makeNewDataSet(xmldoc.Docu
Catch ex As Exception
End Try
Then, in the subroutine "makenewdataset", I try to access values use selectsinglenode to grab field nodes by attribute "name"
n is a node representing a record.
Dim xpath = "/field[@name='first']"
Response.Write("--" & n.SelectSingleNode(xpath).
This for some reason just shuts down the code. It doesn't throw an error. It just mysteriously breaks a loop.
ASKER
you're right. it was throwing an exception.
Object reference not set to an instance of an object.
on this code:
Response.Write("--" & n.SelectSingleNode(xpath). InnerText)
why would it do that?
Object reference not set to an instance of an object.
on this code:
Response.Write("--" & n.SelectSingleNode(xpath).
why would it do that?
n.SelectSingleNode(xpath) might be Nothing
try replacing
Response.Write("--" & n.SelectSingleNode(xpath). InnerText)
with this
Dim node as XmlNode=n.SelectSingleNode (xpath)
If Not node Is Nothing Then
Response.Write("--" & node.InnerText)
End if
Response.Write("--" & n.SelectSingleNode(xpath).
with this
Dim node as XmlNode=n.SelectSingleNode
If Not node Is Nothing Then
Response.Write("--" & node.InnerText)
End if
As YZlat says, its probably not finding the node.
We'd need to see a sample of the XML to see why you're not getting anything.
We'd need to see a sample of the XML to see why you're not getting anything.
ASKER
here's the xml. i just gave you the xml down to relevant node, which is record. it is valid. please ignore the dashes at the beginning of nodes. those are from ieexplorer. you'll see that records are childnodes of resultset. i am passing the resultset node to the madedataset function. thanks to all.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE fmresultset (View Source for full doctype...)>
- <fmresultset xmlns="http://www.filemaker.com/xml/fmresultset" version="1.0">
<error code="0" />
<product build="03/01/2005" name="FileMaker Web Publishing Engine" version="7.0v5" />
<datasource database="elected_official s_sync" date-format="MM/dd/yyyy" layout="elected_officials" table="elected_officials" time-format="HH:mm:ss" timestamp-format="MM/dd/yy yy HH:mm:ss" total-count="7923" />
- <metadata>
<field-definition auto-enter="no" global="no" max-repeat="1" name="ID" not-empty="yes" result="number" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="first" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="last" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="prefix" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="email" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="state" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="LD" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="kind" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="web" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="middle" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="emailMethod" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="emailformstring" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="address1" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="address2" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="city" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="zip" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="phone" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="fax" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="party" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="institution" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="suffix" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="active" not-empty="yes" result="number" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="address_state" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="abbreviation" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="yes" global="no" max-repeat="1" name="creationDate" not-empty="no" result="timestamp" type="normal" />
</metadata>
- <resultset count="7923" fetch-size="25">
- <record mod-id="0" record-id="23232">
- <field name="ID">
<data>400001</data>
</field>
- <field name="first">
<data>James</data>
</field>
- <field name="last">
<data>Elkins</data>
</field>
- <field name="prefix">
<data>Representative</data >
</field>
- <field name="email">
<data>Rep_Jim_Elkins@legis .state.ak. us</data>
</field>
- <field name="state">
<data>AK</data>
</field>
- <field name="LD">
<data>AK_001</data>
</field>
- <field name="kind">
<data>SH</data>
</field>
- <field name="web">
<data>http://w3.legis.state.ak.us/house/24/ELN.php</data>
</field>
- <field name="middle">
<data>B.</data>
</field>
- <field name="emailMethod">
<data>e</data>
</field>
- <field name="emailformstring">
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE fmresultset (View Source for full doctype...)>
- <fmresultset xmlns="http://www.filemaker.com/xml/fmresultset" version="1.0">
<error code="0" />
<product build="03/01/2005" name="FileMaker Web Publishing Engine" version="7.0v5" />
<datasource database="elected_official
- <metadata>
<field-definition auto-enter="no" global="no" max-repeat="1" name="ID" not-empty="yes" result="number" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="first" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="last" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="prefix" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="email" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="state" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="LD" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="kind" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="web" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="middle" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="emailMethod" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="emailformstring" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="address1" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="address2" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="city" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="zip" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="phone" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="fax" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="party" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="institution" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="suffix" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="active" not-empty="yes" result="number" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="address_state" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="no" global="no" max-repeat="1" name="abbreviation" not-empty="no" result="text" type="normal" />
<field-definition auto-enter="yes" global="no" max-repeat="1" name="creationDate" not-empty="no" result="timestamp" type="normal" />
</metadata>
- <resultset count="7923" fetch-size="25">
- <record mod-id="0" record-id="23232">
- <field name="ID">
<data>400001</data>
</field>
- <field name="first">
<data>James</data>
</field>
- <field name="last">
<data>Elkins</data>
</field>
- <field name="prefix">
<data>Representative</data
</field>
- <field name="email">
<data>Rep_Jim_Elkins@legis
</field>
- <field name="state">
<data>AK</data>
</field>
- <field name="LD">
<data>AK_001</data>
</field>
- <field name="kind">
<data>SH</data>
</field>
- <field name="web">
<data>http://w3.legis.state.ak.us/house/24/ELN.php</data>
</field>
- <field name="middle">
<data>B.</data>
</field>
- <field name="emailMethod">
<data>e</data>
</field>
- <field name="emailformstring">
I think your xpath is incorrect
try
Dim xpath = "//field[@name='first']"
Dim node as XmlNode=n.SelectSingleNode (xpath)
If Not node Is Nothing Then
Response.Write("--" & node.InnerText)
End if
Dim xpath = "//field[@name='first']"
Dim node as XmlNode=n.SelectSingleNode
If Not node Is Nothing Then
Response.Write("--" & node.InnerText)
End if
actually use this:
Dim xpath = "//field[@name='first']"
Dim node as XmlNode=n.SelectSingleNode (xpath)
Dim dataNode As XmlNode
If Not node Is Nothing Then
dataNode=node.SelectSingle Node("data ")
Response.Write("--" & dataNode.InnerText)
End if
Dim xpath = "//field[@name='first']"
Dim node as XmlNode=n.SelectSingleNode
Dim dataNode As XmlNode
If Not node Is Nothing Then
dataNode=node.SelectSingle
Response.Write("--" & dataNode.InnerText)
End if
ASKER
no dice. this xpath expression is returning "Nothing" ???
Dim xpath = "//field[@name='first']"
Dim xpath = "//field[@name='first']"
ASKER
the following lines of code work find:
Response.Write(n.ChildNode s.Item(0). Attributes (0).InnerT ext)
Response.Write(n.ChildNode s.Item(0). FirstChild .InnerText )
So, it's something wrong with the selectSingleNode and the xpath expression.
Response.Write(n.ChildNode
Response.Write(n.ChildNode
So, it's something wrong with the selectSingleNode and the xpath expression.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you very much carl.
Can you provide a bit more info ? Maybe the rest of the makeNewDataSet method and a sample of the XML you are using.