When running this VB.net, I get the error "Data at the root level is invalid. Line 1, position 1" when reaching line 3 where it adds the RDF namespace to the Schemaset.
Dim doc As New XmlDocument()
Dim xss As New XmlSchemaSet()
xss.Add("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
doc.Schemas = xss
Dim rdf As XmlElement = doc.CreateElement("rdf:RDF")
rdf.SetAttribute("xmlns", "http://purl.org/rss/1.0/")
doc.AppendChild(rdf)
Debug.WriteLine(doc.ToString)
I am looking for a way to produce the example code for craiglist bulk posting but have had no luck finding .net examples. I am willing to use XML or an RDF library but just can't find good examples of how to create a root element with a colon in it. I found that the above code might be failing because of a .net bug that doesnt permit cdata in the schemaset. Not sure if that is true.
https://www.craigslist.org/about/bulk_posting_interface
<?xml version="1.0"?>
<rdf:RDF xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cl="http://www.craigslist.org/about/cl-bulk-ns/1.0">
<channel>
<items>
<rdf:li rdf:resource="NYCBrokerHousingSample1"/>
<rdf:li rdf:resource="NYCBrokerHousingSample2"/>
</items>
<cl:auth username="listuser@bogus.com"
password="p0stp@rty"
accountID="14"/>
</channel>
...
It is easier and more convenient to build xml feed using XmlWriter
functionality (System.Xml
namespace). This is example for you.
Dim xSet As New System.Xml.XmlWriterSettings()
xSet.Encoding = System.Text.ASCIIEncoding.UTF8
xSet.Indent = True
''xSet.OmitXmlDeclaration = True ''if you wish
Dim sb As New StringBuilder() ''this will keep string.
Dim xw As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sb, xSet) ''StreamBuilder is also possible
xw.WriteStartDocument() '' <?xml...
xw.WriteStartElement("rdf", "RDF", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") ''prefix, localName, NS
xw.WriteAttributeString("xmlns", "", "http://purl.org/rss/1.0/") ''default NS
xw.WriteAttributeString("xmlns", "cl", Nothing, "http://www.craigslist.org/about/cl-bulk-ns/1.0") ''extra NS
xw.WriteStartElement("channel") ''open <channel>
xw.WriteStartElement("items")
xw.WriteStartElement("rdf", "li", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
xw.WriteAttributeString("rdf", "resource", Nothing, "NYCBrokerHousingSample1")
xw.WriteEndElement() ''li
xw.WriteStartElement("rdf", "li", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
xw.WriteAttributeString("rdf", "resource", Nothing, "NYCBrokerHousingSample2")
xw.WriteEndElement() ''li
xw.WriteEndElement() ''items
xw.WriteStartElement("cl", "auth", Nothing)
xw.WriteAttributeString("username", "listuser@bogus.com")
xw.WriteAttributeString("password", "p0stp@rty")
xw.WriteAttributeString("accountID", "14")
xw.WriteEndElement() ''auth
xw.WriteEndElement() ''channel
xw.WriteEndElement() ''RDF
xw.WriteEndDocument()
xw.Flush() ''done
xw.Close() ''cleanup
Return sb.ToString() ''xml string
And this is output:
<?xml version="1.0" encoding="utf-16"?>
<rdf:RDF xmlns="http://purl.org/rss/1.0/"
xmlns:cl="http://www.craigslist.org/about/cl-bulk-ns/1.0"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<channel>
<items>
<rdf:li rdf:resource="NYCBrokerHousingSample1" />
<rdf:li rdf:resource="NYCBrokerHousingSample2" />
</items>
<cl:auth username="listuser@bogus.com" password="p0stp@rty" accountID="14" />
</channel>
</rdf:RDF>