Sql 如何在vb中以字符串形式读取xml文件

Sql 如何在vb中以字符串形式读取xml文件,sql,database,vb.net,onix,Sql,Database,Vb.net,Onix,我试图从一个onix文件中读取信息并将其保存到mysql数据库中 我能够阅读标题、国家代码、isbn和其他字段,但由于一些奇怪的原因,我无法得到简短的描述 简短的描述字段嵌入在html文本中,当我尝试在没有任何更改的情况下从中读取时,它不起作用,如果我尝试将其保存为字符串,也会起同样的作用 在数据库上,我在shortdescription varchar(70)表中创建了一个字段 而且我也,起初认为如果我增加它,它是允许存储的“varchar(70)”的数量,没有帮助 这是我试图阅读的onix提

我试图从一个onix文件中读取信息并将其保存到mysql数据库中

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

简短的描述字段
嵌入在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>

我不确定这是我在代码中做得不对,还是与我在数据库上声明shortdescription的方式有关,假设使用以下XML(以防万一,下面是一个有效的VB.NET声明):

Dim xml=
01

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

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

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

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

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

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

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

非常感谢您的帮助

这就是我的结局

  Dim tes as string = (products.elements(HandleTagName("OtherText"))).Value
  ThisBook.ShortDescription = tes
我点击了这个链接


非常感谢!仅供参考,您不应该使用
new XmlTextReader()
new XmlTextWriter()
。自.NET 2.0以来,它们一直被弃用。请使用
XmlReader.Create()
XmlWriter.Create()
取而代之。谢谢,我会记住这一点,但它确实有效。我的问题是阅读短文description@user3142046嗨,我也在做同样的事情。但是之前,我想知道你是如何构造你的MySQL的?有很多角色、代码等。你在什么地方找到一个结构化的吗?不完全是……我得到的xml不是代码的一部分。我会得到一个n onix文件包含所有xml,然后我试图编写一个程序来读取该文件并将其保存到数据库。@user3142046:好的,请详细说明。@user3142046:哦,不管它是否是代码的一部分,上面的方法是处理任何xml元素的通用方法。另外-请参阅我的编辑,我在那里修复了我的打字错误。请在您的问题中发布任何示例,我不认为注释系统会正确地显示它们。@user3142046:好吧,我只是向您展示了如何从CDATA元素中提取字符串。将其保存到任何位置,就像保存任何其他字符串一样。@user3142046:这对我来说很好,
只是语法上的糖分。但我仍然认为您需要
值。ToString
,而不是
ToString
。区别在于,
ToString
将按原样为您提供一个XML节点,而
值。ToString
将为您提供内容。我希望将CDATA作为字符串读取。如果我说:dim shortd AS string=“test”ThisBook.shortDescription=shortd在数据库中,在shortDescription字段中我可以看到单词“test”,但当我尝试将它保存为isbn dim shortd as string=ThisOtherText.Element(HandleTagName(“Text”))ThisBook.shortDescription=shortd它不起作用。我的问题是如何在vb.netOk中将CDATA保存为字符串。我将作为另一个答案回答。谢谢,我将尝试此欢迎。告诉它是否有帮助。我很高兴你找到了解决方案。请你接受或投票支持我的答案,认为它有帮助吗?它看起来最接近实际情况您的最终结果,在列出的结果中。这是StackOverflow的标准做法。谢谢。大家好,我想知道如何进行投票,您的答案很有帮助!您可以通过单击答案左侧的复选标记来接受答案。复选标记将变为绿色,表示您刚刚接受了答案。您可以进一步投票此答案或任何其他答案在你赢得足够的声誉后,她会回答你(15)。也可以看看这个
  Dim tes as string = (products.elements(HandleTagName("OtherText"))).Value
  ThisBook.ShortDescription = tes