gpt4 book ai didi

c# - XML 序列化 : System. InvalidOperationException: 不是预期的

转载 作者:数据小太阳 更新时间:2023-10-29 02:43:22 29 4
gpt4 key购买 nike

我正在尝试反序列化 XML 文档,当反序列化程序在文档的中途遇到某个标记时,它会给出错误:

System.InvalidOperationException <mytagname> was not expected.

它在类中的前面有一个 [System.Xml.Serialization.XmlArrayItemAttribute("MyTagName", typeof(MediaFile))] 标记,错误只发生在这个特定的标签/类,但我找不到任何会导致这种情况发生的不同之处。有人见过这个吗?

编辑更多细节:

这是反序列化器代码:

 String xmlString = _doc.ToString();
StringReader sr = new StringReader(xmlString);
XmlReader xr = XmlReader.Create(sr);
xs = new XmlSerializer(typeof(VideoAdServingTemplate<AdNode>));
objVast = (VideoAdServingTemplate<AdNode>)xs.Deserialize(sr);

有问题的 XML 如下所示:

<VideoAdServingTemplate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vast.xsd">
<Ad id="myad">
<InLine>
<AdSystem>MyAdSystem</AdSystem>
<AdTitle>Advertisement</AdTitle>
<Description>Shamwow</Description>
<Survey>
<URL><![CDATA[http://www.dynamiclogic.com/tracker?campaignId=234&site=yahoo]]></URL>
</Survey>
<Error>
<URL><![CDATA[http://www.primarysite.com/tracker?noPlay=true&impressionTracked=false]]></URL>
</Error>
<Impression>
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?imp]]></URL>
<URL id="anotheradsever"><![CDATA[http://www.thirdparty.com/tracker?imp]]></URL>
</Impression>
<TrackingEvents>
<Tracking event="start">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?start]]></URL>
</Tracking>
<Tracking event="midpoint">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?mid]]></URL>
<URL id="anotheradsever"><![CDATA[http://www.thirdparty.com/tracker?mid]]></URL>
</Tracking>
<Tracking event="firstQuartile">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?fqtl]]></URL>
<URL id="anotheradsever"><![CDATA[http://www.thirdparty.com/tracker?fqtl]]></URL>
</Tracking>
<Tracking event="thirdQuartile">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?tqtl]]></URL>
<URL id="anotheradsever"><![CDATA[http://www.thirdparty.com/tracker?tqtl]]></URL>
</Tracking>
<Tracking event="complete">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?comp]]></URL>
<URL id="anotheradsever"><![CDATA[http://www.thirdparty.com/tracker?comp]]></URL>
</Tracking>
<Tracking event="mute">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?mute]]></URL>
</Tracking>
<Tracking event="pause">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?pause]]></URL>
</Tracking>
<Tracking event="replay">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?replay]]></URL>
</Tracking>
<Tracking event="fullscreen">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?full]]></URL>
</Tracking>
<Tracking event="stop">
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?stop]]></URL>
</Tracking>
</TrackingEvents>
<Video>
<Duration>00:00:15</Duration>
<AdID>AdID</AdID>
<VideoClicks>
<ClickThrough>
<URL id="myadsever"><![CDATA[http://www.primarysite.com/tracker?click]]></URL>
</ClickThrough>
<ClickTracking>
<URL id="anotheradsever"><![CDATA[http://www.thirdparty.com/tracker?click]]></URL>
<URL id="athirdadsever"><![CDATA[http://www.thirdparty.com/tracker?click]]></URL>
</ClickTracking>
<CustomClick>
<URL id="redclick"><![CDATA[http://www.thirdparty.com/tracker?click]]></URL>
<URL id="blueclick"><![CDATA[http://www.thirdparty.com/tracker?click]]></URL>
</CustomClick>
</VideoClicks>
<MediaFiles>
<MediaFile delivery="streaming" bitrate="250" width="200" height="200" type="video/x-flv">
<URL><![CDATA[rtmp://streamingserver/streamingpath/medium/filename.flv]]></URL>
</MediaFile>
<MediaFile delivery="progressive" bitrate="400" width="200" height="200" type="video/x-flv">
<URL><![CDATA[http://progressive.hostlocation.com//high/filename.flv]]></URL>
</MediaFile>
<MediaFile delivery="progressive" bitrate="200" width="200" height="200" type="video/x-flv">
<URL><![CDATA[http://progressive.hostlocation.com/progressivepath/medium/filename.flv]]></URL>
</MediaFile>
<!-- and a few more MediaFile tags -->
</MediaFiles>
</Video>

XML 的内容有点多,但它很大,所以我从中删减了很多。我不确定反序列化所有内容的顺序,我所知道的是我在调用 xs.Deserialize() 后看到的第一个错误是:

Unhandled Error in Silverlight 2 Application 
Code: 4004
Category: ManagedRuntimeError
Message: System.InvalidOperationException: There is an error in XML document (1, 7). ---> System.InvalidOperationException: <MediaFile xmlns=''> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderVideoAdServingTemplate1.Read27_VideoAdServingTemplate()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, Object events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)

没有迹象表明这是预期的或为什么这不是预期的。

实现它的类具有将标签与对象相关联的定义:

 [System.Xml.Serialization.XmlArrayItemAttribute("MediaFile", typeof(MediaFile))]
public MediaFile[] MediaFiles
{
get
{
return this.mediaFilesField;
}
set
{
this.mediaFilesField = value;
}
}

我假设定义中的[]和XmlArrayItemAttribute的使用是因为连续有多个MediaFile标签,所以它们被放在一个数组中。我用 xsd.exe 生成了这段代码并对其进行了轻微修改以在 Silverlight 中工作。

最佳答案

您可能需要将 MediaFile 类型作为 KnownType 传递给序列化程序:

var knownTypes = new Type[] { typeof(MediaFile) };
xs = new XmlSerializer(typeof(VideoAdServingTemplate<AdNode>), knownTypes);

否则序列化器不知道这个新类型。

关于c# - XML 序列化 : System. InvalidOperationException:<tagname> 不是预期的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/995727/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com