The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

ASP.NET VB.NET XML error trying to get the list of lists

Highlighted
New Member

ASP.NET VB.NET XML error trying to get the list of lists

I have several lists and I have to add a contact by first locating a list by its ShortName in the collection of lists, then adding the contact to that list.


I am able to get the proper xml response in the string strResponse but when I try to create a xPathDocument doc = New XPathDocument(strResponse), I get the exception error: Illegal characters in path. Can someone point me in the right direction please?


Below is my code. Please keep in mind that I am new to XML manipulation :)


Thanks!


' Get the identification info

Dim sUsername As String = System.Configuration.ConfigurationManager.AppSettings("username")

Dim sPassword As String = System.Configuration.ConfigurationManager.AppSettings("Password")

Dim sAPIKey As String = System.Configuration.ConfigurationManager.AppSettings("APIKey")


 'setup httpWebReqeust to send the request

Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists"

Dim address As New Uri(sUri)


'Send the request

Dim theRequest As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)

theRequest.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)

theRequest.Method = "GET"

theRequest.ContentType = "application/x-www-form-urlencoded"


'get the xml response

Dim strResponse As String = String.Empty

Using theResponse As HttpWebResponse = TryCast(theRequest.GetResponse(), HttpWebResponse)

Dim reader As StreamReader = New StreamReader(theResponse.GetResponseStream())

strResponse = reader.ReadToEnd()

reader.Close()


'Handle the xml with xPath

            Dim doc As XPathDocument

            Dim nsmgr As XmlNamespaceManager

            Dim navigator As XPathNavigator

            Dim nodes As XPathNodeIterator

            Dim node As XPathNavigator



            ' Create a new XmlDocument  - THIS IS WHERE I GET THE RUNTIME ERROR

            doc = New XPathDocument(strResponse) 'Illegal characters in path



            ' Create navigator 

            navigator = doc.CreateNavigator()



            ' Set up namespace manager for XPath 

            nsmgr = New XmlNamespaceManager(navigator.NameTable)

            nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")



            ' Get nodes with XPath 

            nodes = navigator.Select("//atom:entry", nsmgr)

2 REPLIES 2
Highlighted
Employee

Re: ASP.NET VB.NET XML error trying to get the list of lists

Hello,



I do have to preface that I am not a Visual Basic developer, but I was able to get the XML stored into an XpathDocument object by changing some of the syntax in your code.  I have posted my changes below:

        ' Get the identification info

        Dim sUsername As String = "username"

        Dim sPassword As String = "Password"

        Dim sAPIKey As String = "APIKey"

        'setup httpWebReqeust to send the request

        Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & sUsername & "/lists"

        Dim address As New Uri(sUri)

        'Send the request

        Dim theRequest As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)

        theRequest.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)

        theRequest.Method = "GET"

        theRequest.ContentType = "application/x-www-form-urlencoded"

        'get the xml response

        Dim strResponse As String = String.Empty

        Using response As HttpWebResponse = theRequest.GetResponse()



            Dim al As List(Of String) = New List(Of String)()

            Dim xpathselect As String = "//at:entry"

            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())

            Dim xmlreader As XmlTextReader = New XmlTextReader(reader)

            Dim doc As XPathDocument = New XPathDocument(xmlreader)

            Dim pn As XPathNavigator = doc.CreateNavigator()



            Dim ns As XmlNamespaceManager = New XmlNamespaceManager(pn.NameTable)



            ns.AddNamespace("at", "http://www.w3.org/2005/Atom")

            ns.AddNamespace("cc", "http://ws.constantcontact.com/ns/1.0")



            Dim nodes As XPathNodeIterator = pn.Select(XPathSelect, ns)



This stores the XML into the variable doc, and you should be able to get the names of the lists from that XML.



If you have any further questions please let me know.



Regards,

Benjamin Soder
NOC Analyst
Constant Contact
Highlighted
New Member

Works perfectly. Thanks!

Works perfectly. Thanks!

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured