0

我正在尝试从 onix 文件中读取并将信息保存到 mysql 数据库中。

我能够阅读标题、国家代码、isbn 和其他字段,但由于某些奇怪的原因,我无法得到简短的描述。

简短的描述字段<d102><d104>嵌入在 html 文本中,当我尝试从它读取而不进行任何更改时,它不起作用,如果我尝试将其保存为字符串也会这样做。

在数据库上,我在表 shortdescription varchar(70) 中创建了一个字段,而且我起初还认为如果我增加它允许存储“varchar(70)”的数量,这无济于事!

这是我正在尝试阅读的 onix 提要的一部分

<othertext>
      <d102>01</d102>
      <d104><![CDATA[A Course in Behavioral Economics  is a concise and reader-friendly      
introduction to one of the most influential areas of economics today. Covering all core areas of the subject, the book requires no advanced mathematics and is full of examples, exercises, and problems drawn from the fields of economics, management, marketing, political science, and public policy, among others. It is an ideal first textbook for students coming to behavioral economics from a wide range of disciplines, and would also appeal to the general reader looking for a thorough and readable introduction to the subject.

Available to lecturers: access to an Instructor's Manual at www.palgrave.com/economics/angner, containing a sample syllabus,
instructor guide, sample handouts and examinations, and PowerPoint slides.]]></d104>
</othertext>

我尝试使用下面的代码,同样的理论适用于获取 isbn 等:

Function HandleTagName(name as String) as XName 
    Select Case name
        Case "d104", "text"
            If ShortName Then
                Return "d104"
            Else
                Return "text"
            End If
     end select 
end function 

dim xmlDoc as XDocument
dim xr as XmlTextReader = new XmlTextReader(Server.MapPath(ThisBook.FromFile))
xr.Namespaces = false

dim  document as XmlDocument = new XmlDocument()
document.Load(xr)
xr.close()
    if not document("ONIXmessage") is Nothing then
        Dim attrColl as XmlAttributeCollection = document("ONIXmessage").Attributes
        attrColl.RemoveAll()
    end if

xmlDoc = XDocument.Parse(document.OuterXML)

Dim Products As IEnumerable(Of XElement)

if xmlDoc.DocumentType is Nothing then
     ShortName = True
else
     if instr(xmlDoc.DocumentType.ToString(), "/short") then
          ShortName = True
     end if
end if

Products = from product in xmlDoc.Root.Descendants(HandleTagName("Product"))

For Each ThisOtherText In product.Elements(HandleTagName("OtherText"))
    If ThisOtherText.Element(HandleTagName("TextTypeCode")) = "02" Then         
        If ThisBook.shortDescription = "" Then 
           ' if you say 
           ' dim xxx as string = "test"
           ' ThisBook.shortDescription = xxx

           ThisBook.shortDescription = ThisOtherText.Element(HandleTagName("Text"))
        End if 
    End If
Next

我不确定这是我在代码中做的不对还是与我在数据库上声明简短描述的方式有关

4

4 回答 4

1

哦,您只想将 .xml 文件读入 VB.NET 字符串吗?

Imports System.IO
...
Dim xmlfilereader as streamreader = new streamreader("locationofxmlfile.xml")
dim xmlfilestring as string = xmlfilereader.read()

xmlfilestring 现在是一个包含您的 XML 文件的字符串。这就是你想要的吗?

于 2013-12-29T20:43:54.827 回答
0

你可以使用 Dataset 来读取 XML:

    Dim ds As New DataSet
    Dim myString As String
    ds.ReadXml("Your Xml Path")
    myString = ds.Tables(0).Rows(0)("d104")
于 2013-12-30T08:09:08.763 回答
0

假设以下 XML(以防万一,下面是有效的 VB.NET 声明):

Dim xml = <othertext>
            <d102>01</d102>
            <d104><![CDATA[A Course in Behavioral Economics  is a concise and reader-friendly      
introduction to one of the most influential areas of economics today. Covering all core areas of the subject, the book requires no advanced mathematics and is full of examples, exercises, and problems drawn from the fields of economics, management, marketing, political science, and public policy, among others. It is an ideal first textbook for students coming to behavioral economics from a wide range of disciplines, and would also appeal to the general reader looking for a thorough and readable introduction to the subject.

Available to lecturers: access to an Instructor's Manual at www.palgrave.com/economics/angner, containing a sample syllabus,
instructor guide, sample handouts and examinations, and PowerPoint slides.]]></d104>
          </othertext>

这个:

xml.<d104>.Value.ToString

产生一个字符串:

行为经济学课程是对当今最具影响力的经济学领域之一的简明易读的介绍。这本书涵盖了该主题的所有核心领域,不需要高等数学,并且充满了来自经济学、管理学、市场营销、政治学和公共政策等领域的示例、练习和问题。对于从广泛学科学习行为经济学的学生来说,它是理想的第一本教科书,并且也吸引了寻求对该主题进行全面且易读的介绍的普通读者。讲师可用:访问 www.palgrave.com/economics/angner 上的讲师手册,其中包含示例教学大纲、讲师指南、示例讲义和考试以及 PowerPoint 幻灯片。

那是你要的吗?如果没有,请在评论中告诉我。

于 2013-12-29T17:33:20.387 回答
0

假设创建了一个名为 xmldata 的变量,其中包含标记内的所有 xml 文件,作为一个字符串。希望你知道如何达到这一点。从那里,您需要根据 '[' 字符将字符串拆分为一个数组。使用以下行:

Dim Openbracketarray() as string = xmldata.split("[")

接下来,您需要找到 CDATA 部分。使用以下脚本:

Dim locationofCDATA as integer = 0 '0 just in case theres an error.'
For i = 0 to Openbracketarray.length - 1 'loop through all instances of a bracket found in the xml'
If Openbracketarray(i) = "CDATA"
locationofCDATA = i
EndIf
i = i + 1
Next

现在,使用该位置,找到 CDATA 字符串的内容。

Dim CDATA as string = Openbracketarray(location of CDATA + 1)

但是等等,我们最后还有所有的行话。用另一个拆分删除它。

CDATA = CDATA.Split("]")(0)

这将删除 CDATA 字符串后的右括号。

于 2013-12-30T22:13:39.927 回答