我想从原子feed / xml文件中获取一些行并放入数据库。我知道如何将字符串输入数据库,但我很难理解如何使用xml文件,因为这是我第一次。 让我们说xml文件是“C:\ feed.xml”,看起来像这样..
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>This is the Title</title>
<link href="https://www.website.com"></link>
<entry>
<title>Title 1</title>
<link href="http://www.website.com/Title/page1"></link>
</entry>
<entry>
<title>Title 2</title>
<link href="http://www.website.com/Title/page2"></link>
</entry>
</feed>
伪代码......
Dim doc As XDocument = XDocument.Load("C:\feed.xml")
For i = 0 to entry.count - 1
Dim String1 as String = title(i).InnerText
Dim String2 as String = link href(i).InnerText
database.insert(String1, String2)
Next i
不要担心database.insert部分,我知道如何做到这一点,我只需要帮助获取String1和String2。特别是String2因为我需要它只是String2 =“http // www.website.com / Title / page1”(注意:故意缺失)
非常感谢任何帮助。感谢。
答案 0 :(得分:0)
尝试以下xml linq
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:\temp\test.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim feed As XElement = doc.Root
Dim ns As XNamespace = feed.GetDefaultNamespace()
Dim results = doc.Elements(ns + "feed").Select(Function(x) New With { _
.title = CType(x.Element(ns + "title"), String), _
.link = CType(x.Element(ns + "link").Attribute("href"), String), _
.entry = x.Elements(ns + "entry").Select(Function(y) New With {
.title = CType(y.Element(ns + "title"), String), _
.link = CType(y.Element(ns + "link").Attribute("href"), String) _
}).ToList()
}).FirstOrDefault()
End Sub
End Module
答案 1 :(得分:0)
您可以使用SyndicationFeed课程。
添加对System.ServiceModel.dll程序集的引用。
Imports System.ServiceModel.Syndication
Imports System.Xml
Using xmlReader As XmlReader = XmlReader.Create("C:\feed.xml")
Dim feed As SyndicationFeed = SyndicationFeed.Load(xmlReader)
Console.WriteLine(feed.Title.Text)
For Each link In feed.Links
Console.WriteLine(link.Uri)
Next
For Each entry In feed.Items
Console.WriteLine(entry.Title.Text)
For Each link In entry.Links
Console.WriteLine(link.Uri)
Next
Next
End Using