【问题标题】:There is an error in XML document (2, 2)XML 文档中存在错误 (2, 2)
【发布时间】:2011-09-29 14:56:22
【问题描述】:

我已经搜索了类似的问题,但仍然无法弄清楚。

内部异常:{"<xml xmlns=''> was not expected."}

我从在线资源中提取 XML,因此无法对其进行更改。 http://api.ustream.tv/xml/user/techcrunch/listAllVideos?key=devkey

我使用 Visual Studio 2010 从 XML 生成 XSD 文件。我使用 xsd.exe 生成 VB 类。

这是我所有的代码: 表格1:

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:(缩短)

<?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

【问题讨论】:

  • 只发布VB文件的第一位:“Xml”和类以及NewDataSet类的定义。
  • 我担心你想把它当作一个数据集。
  • John xsd.exe 工具生成了一个包含数据集的类。 XML 比我发布的要大得多。您可以在此处查看 XML:api.ustream.tv/xml/user/techcrunch/listAllVideos?key=devkey
  • 我在 John 上方添加了尽可能多的内容,非常感谢您的宝贵时间。
  • John 它使用 xsd.exe 为该类生成了如此多的代码,如果我将代码复制到 microsoft word 中它是 63 页。这似乎有点过头了。这是 xsd.exe 工具的常见行为吗?你能推荐另一种方法吗?

标签: xml-serialization


【解决方案1】:

我不认为xml元素可以有名称“xml”吗?

http://www.w3.org/TR/xml/#sec-common-syn

[定义:名称是具有一组受限初始字符的 Nmtoken。] 名称中不允许使用的初始字符包括数字、变音符号、句号和连字符。

以字符串“xml”开头的名称,或任何匹配 (('X'|'x') ('M'|'m') ('L'|'l')) 的字符串的名称是保留用于本规范本版本或未来版本的标准化。

【讨论】:

  • +1 为什么没有人遵循建议?上面写着无论如何,xml 解析器应该像平常一样解析标记,即使它们包含冒号字符,但是为什么有人应该提供无效的 XML?
  • 是的,可以,但不符合 w3c 标准的想法
【解决方案2】:

问题是您的NewDataSet 类将匹配以&lt;NewDataSet&gt; 开头的XML,但您传递的是以&lt;xml&gt; 开头的XML。这就是它出乎意料的原因。

要么使用&lt;NewDataSet&gt;&lt;/NewDataSet&gt; 包装XML,要么使用xsd /c 命令生成您的类,这将只生成“普通”类而不是数据集。

【讨论】:

    【解决方案3】:

    所有 XML 元素的格式都正确。您可以将它放在 XML 格式验证器中,例如 OygenXML 或您最喜欢的 XML 编辑器来验证它。问题在于您的值,您需要正确编码特殊字符。这是不能在普通 XML 字符串中使用的 XML 特殊字符集的列表。这些字符是:

    1. & - &amp; 
    2. < - &lt; 
    3. > - &gt; 
    4. " - &quot; 
    5. ' - &#39; 
    

    您将需要正确格式化这些值,以避开此错误。我建议使用循环值并使用 System.Security.SecurityElement.Escape(stringValue); 对值进行编码。

    【讨论】:

      猜你喜欢
      • 2013-08-25
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2021-08-28
      • 2019-01-14
      相关资源
      最近更新 更多