我已经搜索了类似的问题,但仍然无法解决这个问题。
内部异常:{"<xml xmlns=''> was not expected."}
我正在从一个在线资源中提取XML,因此无法对其进行更改。http://api.ustream.tv/xml/user/techcrunch/listAllVideos?key=devkey
我使用visual studio 2010从XML生成了XSD文件。我使用xsd.exe生成VB类。
下面是我的所有代码: 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 ClasslistAllVideos.xml:(缩写)
<?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: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>VB类真的很长,不让我发布它。耽误您时间,实在对不起。
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发布于 2011-09-29 23:16:43
我不认为xml元素可以有"xml“这个名字,对吗?
http://www.w3.org/TR/xml/#sec-common-syn
定义:名称是具有一组受限首字母的Name标记。名称不允许使用的首字符包括数字、变音符号、句号和连字符。
以字符串"xml“或任何与(('X'|'x') ('M'|'m') ('L'|'l'))匹配的字符串开头的名称将保留,以便在本规范的此版本或未来版本中实现标准化。
发布于 2011-10-03 22:33:48
问题是您的NewDataSet类将匹配以<NewDataSet>开头的XML,但是您传递的是以<xml>开头的XML。这就是为什么它是出乎意料的。
要么用<NewDataSet></NewDataSet>包装XML,要么使用xsd /c命令生成您的类,该命令将只生成“普通”类,而不生成dataset。
发布于 2013-02-20 03:59:47
所有XML元素的格式都是正确的。您可以将其放在OygenXML或您最喜欢的XML Editor等XML格式验证器中进行验证。问题出在您的值上,您需要对特殊字符进行正确编码。这是不能在普通XML字符串中使用的XML特殊字符集的列表。这些字符是:
1. & - &
2. < - <
3. > - >
4. " - "
5. ' - ' 您将希望正确地格式化这些值,以便超越此错误。我建议使用循环遍历这些值,并使用System.Security.SecurityElement.Escape(stringValue);对这些值进行编码。
https://stackoverflow.com/questions/7599241
复制相似问题