-1

Using VB.NET within VS2008.

Sample XML FIle

<MailListExportXML xsi:schemaLocation="com.efi.planner.mailing.export MonarchMailListExport.xsd" xmlns="com.efi.planner.mailing.export" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MailListFiles>
  <MailListFile MailListID="1" MailListName="Test Mail File 11-14-2013.csv" MailListDateTimeStamp="2013-12-04T19:18:00Z" MailListFileLocationPath="c:ImportedMailFiles" StatedFirstRecord="George Vaisey" StatedLastRecord="Mary Smith" TotalMailListRecordCount="1992"/></MailListFiles>
</MailListExportXML>

I am struggling to get value from MailListName to populate to a text box.

Ken White
  • 123,280
  • 14
  • 225
  • 444
George Vaisey
  • 139
  • 1
  • 5
  • 19

3 Answers3

1

You can try something like this:

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(PATH TO XML FILE)

Dim docNode As XmlNode = xmlDoc.SelectSingleNode("MailListExportXML/MailListFiles/MailListFile[@MailListID=1]")
SOMETEXTBOX.Text = docNode.Attributes.GetNamedItem("MailListName").Value
xmlDoc = Nothing

based on you error, make sure the xmlDoc.Load(... IS THE PATH AND NAME OF THE ACTUAL XML DOCUMENT. below is a MapPath example but you can use the absolute path like "c:\inetpub\wwwroot\webapp\App_Data\Mailing.xml"

Dim xml_doc As XmlDataDocument = New XmlDataDocument()
xml_doc.Load(Server.MapPath("~/App_Data/NAME AND PATH OF XML FILE.xml"))
Dim docNode As XmlNode = xml_doc.SelectSingleNode("MailListExportXML/MailListFiles/MailListFile[@MailList‌​ID=1]")
tbMMInputFile.Text = docNode.Attributes.GetNamedItem("MailListName").Value
xmlDoc = Nothing
mikea80
  • 127
  • 1
  • 5
  • Here is the code I'm using that generates an error about an empty url. Dim xml_doc As XmlDataDocument = New XmlDataDocument xml_doc.Load(tbMMInputFile.Text) Dim docNode As XmlNode = xml_doc.SelectSingleNode("MailListExportXML/MailListFiles/MailListFile[@MailListID=1]") tbMMInputFile.Text = docNode.Attributes.GetNamedItem("MailListName").Value – George Vaisey Jan 27 '14 at 01:08
1

You have a default namespace, so you'll need to specify the namespace in XPath query using XmlNamespaceManager :

Dim doc As XmlDocument = New XmlDocument()
doc.Load("path_to_xml_file.xml")
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
namespaceManager.AddNamespace("d", doc.DocumentElement.NamespaceURI)
Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]")
MyTextBox.Text = node.Attributes.GetNamedItem("MailListName").Value

Reference : [SO Post about XPath query to select nodes having default namespace]

UPDATE :

My bad, I missed one thing, in previous snippet namespaceManager was not used. So as confirmed by OP, this line in previous snippet :

Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]")

should be like this instead (note namespaceManager as second parameter of SelectSingleNode function) :

Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]", namespaceManager)
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • Name space manager worked great but produced an error about an invalid token. I had to change the doc.selectsinglenote to reference the namespacemanager and it worked. Revised line is below.. Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]", namespaceManager) – George Vaisey Jan 27 '14 at 18:01
  • my bad, forgot to use the `namespaceManager`. You are correct, without passing it as parameter of `SelectSingleNode` it is useless to create `namespaceManager`. Updated my answer to clarify that point. – har07 Jan 28 '14 at 00:31
0

Using the example with the "namespacemanager" resolved the problems.

George Vaisey
  • 139
  • 1
  • 5
  • 19