【问题标题】:How to read an xml file as string in vb如何在vb中将xml文件读取为字符串
【发布时间】:2014-01-16 15:12:07
【问题描述】:

我正在尝试从 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

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

【问题讨论】:

  • 仅供参考,您不应使用new XmlTextReader()new XmlTextWriter()。自 .NET 2.0 以来,它们已被弃用。请改用XmlReader.Create()XmlWriter.Create()
  • 谢谢,我会记住这一点,但它确实有效。我的问题是阅读简短说明
  • @user3142046 嗨,我也在做同样的事情。但在此之前,我想知道你是如何构建你的 MySQL 的?有很多角色、代码等。你是否在某个地方找到了一个结构化的?

标签: sql database vb.net onix


【解决方案1】:

假设我们创建了一个名为 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 字符串后的右括号。

【讨论】:

    【解决方案2】:

    你可以使用 Dataset 来读取 XML:

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

    【讨论】:

    • 谢谢,我试试这个
    • 欢迎。告诉它是否有帮助。
    【解决方案3】:

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

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

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

    【讨论】:

    • 我想将 CDATA 读取为字符串。如果我应该说: dim shortd as string = "test" ThisBook.shortDescription = shortd 在数据库上,在 shortdescription 字段中我可以看到单词“test”,但是当我尝试保存它时,就像我为 isbn dim shortd 所做的那样as string = ThisOtherText.Element(HandleTagName("Text")) ThisBook.shortDescription = shortd 它不起作用。我的问题是如何在 vb.net 中将 CDATA 保存为字符串
    • 好的。我将作为另一个答案回复。
    【解决方案4】:

    假设以下 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 幻灯片。

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

    【讨论】:

    • 不完全...。我得到的 xml 不是代码的一部分。我将得到一个包含所有 xml 的 onix 文件,然后我正在尝试编写一个程序来读取该文件并将其保存到数据库中。
    • @user3142046:好的,请详细说明。
    • @user3142046:哦,它是否是代码的一部分并不重要,上述方法是处理任何 XML 元素的通用方法。另外 - 看我的编辑,我在那里修正了我的错字。请在您的问题中发布任何示例,我认为评论系统不会正确显示它们。
    • @user3142046:好吧,我刚刚向您展示了如何从 CDATA 元素中提取字符串。以与保存任何其他字符串相同的方式将其保存到任何位置。
    • @user3142046:这对我来说很好,&lt;&gt; 只是语法糖。但我仍然认为你想要Value.ToString,而不是ToString。不同之处在于,ToString 将按原样为您提供 XML 节点,而 Value.ToString 将为您提供内容。
    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 2011-04-24
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多