gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:07:56 25 4
gpt4 key购买 nike

运行此 VB.net 时,到达第 3 行时出现错误“根级别的数据无效。第 1 行,位置 1”,其中将 RDF 命名空间添加到架构集。

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 错误不允许模式集中的 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>
...

最佳答案

使用 XmlWriter 功能(System.Xml 命名空间)构建 xml 提要更容易、更方便。这是给你的例子。

    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>

关于.net - 如何在 ASP.NET 中为 Craigslist 批量发布创建根 RDF/XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com