There is an error in XML document (2, 2)
I have searched similar questions and still can't figure this out.
Inner Exception: {"<xml xmlns=''> was not expected."}
I am pulling the XML from an online source so cannot make changes to it. http://api.ustream.tv/xml/user/techcrunch/listAllVideos?key=devkey
I generated the XSD file from XML using visual studio 2010. I used xsd.exe to generate the VB class.
Here is all of my code: Form1:
Imports System.Xml
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Public Class Form1
Private Const strURL As String = "http://api.ustream.tv/xml/user/techcrunch/listAllVideos?key=devkey"
Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
Dim ds As New NewDataSet()
Dim myReader As TextReader = New StreamReader("../../listAllVideos.xml")
Dim serializer As New XmlSerializer(GetType(NewDataSet))
ds = DirectCast(serializer.Deserialize(myReader), NewDataSet)
myReader.Close()
End Sub
End Class
listAllVideos.xml: (shortened)
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<results>
<array key="0">
<id>1916735</id>
<title><![CDATA[The TechCrunch Office live 8/2/09 04:50PM]]></title>
<protected>FALSE</protected>
<description><![CDATA[8/2/09 04:50PM PST]]></description>
<createdAt><![CDATA[2009-08-02 16:开发者_StackOverflow社区13:54]]></createdAt>
<rating>3.000</rating>
<lengthInSecond>2146.777</lengthInSecond>
<totalViews>1943</totalViews>
<codecIsForLiveHttp>0</codecIsForLiveHttp>
<serverId>992</serverId>
<videoName><![CDATA[1_170_1249254834012]]></videoName>
<url><![CDATA[http://www.ustream.tv/recorded/1916735]]></url>
<embedTag><![CDATA[<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="320" height="260" id="utv445187" name="utv_n_638715"><param name="flashvars" value="autoplay=false&vid=1916735" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.ustream.tv/flash/viewer.swf" /><embed flashvars="autoplay=false&vid=1916735" width="320" height="260" allowfullscreen="true" allowscriptaccess="always" id="utv445187" name="utv_n_638715" src="http://www.ustream.tv/flash/viewer.swf" type="application/x-shockwave-flash" /></object>]]></embedTag>
<liveHttpUrl><![CDATA[]]></liveHttpUrl>
<imageUrl>
<small><![CDATA[http://static-cdn2.ustream.tv/videopic/0/1/1/1916/1916735/1_170_1916735_120x90_b_1:1.jpg]]></small>
<medium><![CDATA[http://static-cdn2.ustream.tv/videopic/0/1/1/1916/1916735/1_170_1916735_320x240_b_1:1.jpg]]></medium>
</imageUrl>
<sourceChannel>
<id>170</id>
<url><![CDATA[http://www.ustream.tv/channel/170]]></url>
</sourceChannel>
</array>
</results>
<msg><![CDATA[]]></msg>
<error><![CDATA[]]></error>
<processTime>TRUE</processTime>
<version><![CDATA[mashery-r10]]></version>
</xml>
listAllVideos.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="xml">
<xs:complexType>
<xs:sequence>
<xs:element name="results">
<xs:complexType>
<xs:sequence>
<xs:element name="array">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:unsignedInt" />
<xs:element name="title" type="xs:string" />
<xs:element name="protected" type="xs:string" />
<xs:element name="description" type="xs:string" />
<xs:element name="createdAt" type="xs:string" />
<xs:element name="rating" type="xs:decimal" />
<xs:element name="lengthInSecond" type="xs:decimal" />
<xs:element name="totalViews" type="xs:unsignedShort" />
<xs:element name="codecIsForLiveHttp" type="xs:unsignedByte" />
<xs:element name="serverId" type="xs:unsignedShort" />
<xs:element name="videoName" type="xs:string" />
<xs:element name="url" type="xs:string" />
<xs:element name="embedTag" type="xs:string" />
<xs:element name="liveHttpUrl" type="xs:string" />
<xs:element name="imageUrl">
<xs:complexType>
<xs:sequence>
<xs:element name="small" type="xs:string" />
<xs:element name="medium" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sourceChannel">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:unsignedByte" />
<xs:element name="url" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="key" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="msg" type="xs:string" />
<xs:element name="error" type="xs:string" />
<xs:element name="processTime" type="xs:string" />
<xs:element name="version" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The VB class is really long and it won't let me post it. Thanks for your time.
Partial Public Class NewDataSet
Inherits Global.System.Data.DataSet
Private tablexml As xmlDataTable
Private tableresults As resultsDataTable
Private tablearray As arrayDataTable
Private tableimageUrl As imageUrlDataTable
Private tablesourceChannel As sourceChannelDataTable
Private relationxml_results As Global.System.Data.DataRelation
Private relationresults_array As Global.System.Data.DataRelation
Private relationarray_imageUrl As Global.System.Data.DataRelation
Private relationarray_sourceChannel As Global.System.Data.DataRelation
Private _schemaSerializationMode As Global.System.Data.SchemaSerializationMode = Global.System.Data.SchemaSerializationMode.IncludeSchema
I don't think an xml element can have the name "xml" can it?
http://www.w3.org/TR/xml/#sec-common-syn
[Definition: A Name is an Nmtoken with a restricted set of initial characters.] Disallowed initial characters for Names include digits, diacritics, the full stop and the hyphen.
Names beginning with the string "xml", or with any string which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for standardization in this or future versions of this specification.
The problem is that your NewDataSet
class would match XML starting with <NewDataSet>
, but you're passing XML that starts with <xml>
. That's why it's unexpected.
Either wrap the XML with <NewDataSet></NewDataSet>
, or else generate your classes by using the xsd /c
command, which will only generate "plain" classes and not a dataset.
All of your XML Elements are correctly formatted. You can place this in an XML format validator such as OygenXML or your favorite XML Editor to validate that. The issue is with your values, you will need to encode the special characters correctly. This is a list of XML's special set of characters that cannot be used in normal XML strings. These characters are:
1. & - &
2. < - <
3. > - >
4. " - "
5. ' - '
You will want to format the values correctly in order to get beyond this error. I suggest using looping through the values and using System.Security.SecurityElement.Escape(stringValue); to encode the values.
精彩评论