简体   繁体   中英

Parsing XML in vb.net

I have some XML that a vendor is supplying me and I can not parse it in VB.net no matter what I try. I am a novice at XML so please be understanding of that.

This is the xml that the vendor sent me:

<?xml version='1.0'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xsd:element name="event">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="id" type="xsd:integer"></xsd:element>
   <xsd:element name="old_id" type="xsd:integer"></xsd:element>
   <xsd:element name="opponent" type="xsd:string">Austin MS</xsd:element>
   <xsd:element name="game_date" type="xsd:string">2011-08-31</xsd:element>
   <xsd:element name="start_time" type="xsd:string">5:00PM</xsd:element>
   <xsd:element name="end_time" type="xsd:string">8:00PM</xsd:element>
   <xsd:element name="sport" type="xsd:string">Baseball</xsd:element>
   <xsd:element name="level" type="xsd:string">7th</xsd:element>
   <xsd:element name="gender" type="xsd:string">Boys</xsd:element>
   <xsd:element name="year" type="xsd:string">2011</xsd:element>
   <xsd:element name="season" type="xsd:string">Fall</xsd:element>
   <xsd:element name="status" type="xsd:string">Deleted</xsd:element>
   <xsd:element name="homeaway" type="xsd:string">Home</xsd:element>
   <xsd:element name="facility">France</xsd:element>
   <xsd:element name="facility_id" type="xsd:integer"></xsd:element>
   <xsd:element name="opponent">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="ss_id" type="xsd:integer">19505</xsd:element>
      <xsd:element name="name" type="xsd:string">Austin MS</xsd:element>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="location">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="ss_id" type="xsd:integer">108</xsd:element>
      <xsd:element name="name" type="xsd:string">Breck School</xsd:element>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="score">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="result" type="xsd:string"></xsd:element>
      <xsd:element name="ours" type="xsd:string"></xsd:element>
      <xsd:element name="theirs" type="xsd:string"></xsd:element>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="league" type="xsd:string"></xsd:element>
   <xsd:element name="type" type="xsd:string">Championship</xsd:element>
   <xsd:element name="ss_id" type="xsd:string">305</xsd:element>
   <xsd:element name="transportation" type="xsd:string"></xsd:element>
   <xsd:element name="dismissal" type="xsd:string"></xsd:element>
   <xsd:element name="return" type="xsd:string"></xsd:element>
   <xsd:element name="comment">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="school" type="xsd:string"></xsd:element>
      <xsd:element name="conference" type="xsd:string"></xsd:element>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="home_team" type="xsd:string">Breck - St Paul - Apple Valley</xsd:element>
   <xsd:element name="activity_type" type="xsd:string">conference</xsd:element>
   <xsd:element name="category" type="xsd:string"></xsd:element>
   <xsd:element name="sub_category" type="xsd:string"></xsd:element>
   <xsd:element name="state" type="xsd:string">MN</xsd:element>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>

</xsd:schema>

I have tried to use XSD.exe to create a vb.net class and it errors. I have contacted the vendor who supplied the XML but they haven't been much help. I would like to be able to pump this XML into a function and have it pop out a list(of SportEvents) with the properties and lists that are in the XML.

I would be greatful for any help in pointing me in the right direction. Thanks

The method that I have used is to implement the System.Xml functionality http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx

Dim configDoc As XmlDocument = New XmlDocument()
Dim nodes As XmlNodeList = Nothing
Dim stringHolder as String = ""

you load the document like such

configDoc.Load("filepath") 'load the file

then pull in the values either single node or list of nodes

stringHolder = configDoc.SelectSingleNode("/xpath").attributes.getNamedItem("attributeName").Value.toString()

or for a list of inner nodes.

nodes = configDoc.SelectNodes("/xpath")
for each n as XmlNode in nodes
  stringHolder = n.attributes.getNamedItem("attributeName").Value.toString()
'do stuff with string
next

now these ways only deal with attributes of xml nodes. if you need the inner text then you can research .innertext() its very simmiler. hope this helps to at least get you pointed in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM