如何在ASP.NET中为Craigslist批量发布创建根RDF / XML元素

时间:2016-06-29 02:02:35

标签: .net xml vb.net craigslist rdf-xml

运行这个VB.net时,我收到错误"根级别的数据无效。第1行,第1和第34行;到达第3行时,它将RDF命名空间添加到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)

我正在寻找一种方法来生成craiglist批量发布的示例代码,但没有找到.net示例。我愿意使用XML或RDF库,但却找不到如何创建带冒号的根元素的好例子。我发现上面的代码可能会失败,因为.net错误不允许在schemaset中使用cdata。不确定是否属实。

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>
...

1 个答案:

答案 0 :(得分:3)

使用XmlWriter功能(System.Xml命名空间)构建xml Feed更简单,更方便。这是你的榜样。

    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

这是输出:

<?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>