如何在vb中将xml文件读取为字符串

时间:2013-12-29 16:48:42

标签: sql database vb.net onix

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

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

简短描述字段<d102><d104>嵌入在html文本中,当我尝试从中读取而没有任何替换时它不起作用,如果我尝试将其保存为字符串也是如此。

在数据库中我在表中创建了一个字段shortdescription varchar(70) 而且我也一开始认为它是允许存储“varchar(70)”的数量,如果我增加它,没有帮助!

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

<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

我不确定它是否在代码中做得不对,或者它与我如何在数据库中声明短描述有关

5 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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幻灯片。

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

答案 2 :(得分:0)

您可以使用数据集读取XML:

    Dim ds As New DataSet
    Dim myString As String
    ds.ReadXml("Your Xml Path")
    myString = ds.Tables(0).Rows(0)("d104")

答案 3 :(得分: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字符串后的近括号。

答案 4 :(得分:0)

非常感谢你的帮助。

这就是我最终的结果

  Dim tes as string = (products.elements(HandleTagName("OtherText"))).Value
  ThisBook.ShortDescription = tes

我按照这个链接
     http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.value(v=vs.110).aspx

非常感谢!