How to read an xml file as string in vb

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


I'm trying to read from an onix file and save the information onto a mysql database.

I'm able to read titles, country code, isbn and other fields but for some strange reason I cant get short description.

The short description field  is embed in html text and when I try to read from it without any alternations it doesnt work and if i tried to save it as a string does the same.

On the database I created a field in the table shortdescription varchar(70) and i also, at first thought it was the amount its allowed to stored "varchar(70)" if I increase it, doesn't help!

This is part of an onix feed I'm trying to read


      01
      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.]]>

I tried using this code below, the same theory worked for getting the isbn etc.:

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

I'm not sure if its something i'm not doing right in the code or is it something to do with how i declared shortdescription on the database

profilepic.png
manpreet 2 years ago

Oh, is all you want to do read a .xml file into a VB.NET string?

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

xmlfilestring is now a string containing your XML file. Is that all you wanted?


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.