masteroleary masteroleary - 1 year ago 113 Question

How to create a root RDF/XML element for Craigslist Bulk Posting in ASP.NET

When running this, 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", "")
doc.Schemas = xss
Dim rdf As XmlElement = doc.CreateElement("rdf:RDF")
rdf.SetAttribute("xmlns", "")

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.

<?xml version="1.0"?>

<rdf:RDF xmlns=""

<rdf:li rdf:resource="NYCBrokerHousingSample1"/>
<rdf:li rdf:resource="NYCBrokerHousingSample2"/>

<cl:auth username=""

Answer Source

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", "") ''prefix, localName, NS
    xw.WriteAttributeString("xmlns", "", "") ''default NS
    xw.WriteAttributeString("xmlns", "cl", Nothing, "") ''extra NS
    xw.WriteStartElement("channel") ''open <channel>

    xw.WriteStartElement("rdf", "li", "")
    xw.WriteAttributeString("rdf", "resource", Nothing, "NYCBrokerHousingSample1")
    xw.WriteEndElement() ''li

    xw.WriteStartElement("rdf", "li", "")
    xw.WriteAttributeString("rdf", "resource", Nothing, "NYCBrokerHousingSample2")
    xw.WriteEndElement() ''li

    xw.WriteEndElement() ''items

    xw.WriteStartElement("cl", "auth", Nothing)
    xw.WriteAttributeString("username", "")
    xw.WriteAttributeString("password", "p0stp@rty")
    xw.WriteAttributeString("accountID", "14")
    xw.WriteEndElement() ''auth

    xw.WriteEndElement() ''channel
    xw.WriteEndElement() ''RDF
    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="" 
      <rdf:li rdf:resource="NYCBrokerHousingSample1" />
      <rdf:li rdf:resource="NYCBrokerHousingSample2" />
    <cl:auth username="" password="p0stp@rty" accountID="14" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download