反序列化错误:从命名空间'的XML元素'名'已经存在于当前范围 [英] Deserialization Error: The XML element 'name' from namespace '' is already present in the current scope

查看:565
本文介绍了反序列化错误:从命名空间'的XML元素'名'已经存在于当前范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用XML序列化我的第一次,这是推动我绝对坚果后,试图解决此2天。



我得到这个错误时,在反序列化踢:

  XML元素名称从命名空间'已经存在于当前范围。使用XML属性来指定元素另一种XML名称或名称空间。 



在此行中我的​​代码发生错误:

 专辑专辑=(专辑)serializer.Deserialize(读卡器); 



我不知道为什么。没有DUP名节点所以我只是不明白这一点。这是由第三方REST API从HttpWebResponse接收的XML文档



下面是完整的代码:



我的相册类(类型,我要反序列化):

 公共类专辑
{
#区域构造

公共专辑()
{

}

#endregion

#地区ElementConstants

公共静态类ElementConstants
{
公共常量字符串援助=援助;
公共常量字符串所有者=所有者;
公共常量字符串ALBUMNAME =名;
公共常量字符串CoverPhotoID =cover_pid;
公共常量字符串CREATEDATE =创造;
公共常量字符串LastModifiedDate =修改;
公共常量字符串描述=说明;
公共常量字符串位置=地利;
公共常量字符串AlbumURL =联系;
公共常量字符串大小=大小;
公共常量字符串可见=看得见;
}

#endregion ElementConstants

#地区的公共属性

[XmlArray(的ElementName =photos_GetAlbums_response)]
[XmlArrayItem(专辑)]
公共相册[] {专辑获得;组; }

[的XmlElement(的ElementName = ElementConstants.AlbumName,数据类型=字符串)]
公共字符串ALBUMID {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.aID,数据类型=INT)]
公众的Int32 CoverPhotoID {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Owner,数据类型=字符串)]
公共字符串所有者获得{;组; }

[的XmlElement(的ElementName = ElementConstants.AlbumName,数据类型=字符串)]
公共字符串ALBUMNAME {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.aID,数据类型=日期时间)]
公众的DateTime CREATEDATE {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.LastModifiedDate,数据类型=日期时间)]
公众的DateTime LastModifiedDate {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Description,数据类型=字符串)]
公共字符串描述{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Location,数据类型=字符串)]
公共字符串位置{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.AlbumURL,数据类型=字符串)]
公共字符串链接{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Size,数据类型=大小)]
公共字符串大小{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Visible,数据类型=字符串)]
公共字符串可见{搞定;组; }

#endregion
}

我的串行器类

 公共类串行
{
公共静态专辑CreateAlbumFromXMLDoc(XmlDocument的DOC)
{
//创建一个串行
无功序列化=新的XmlSerializer(typeof运算(专辑))的实例;
变种读者=新StringReader(doc.ToString());

//反序列化的XML对象,并转换为类型专辑
专辑专辑=(专辑)serializer.Deserialize(读卡器);

返回专辑;
}
}



的XML,我想反序列化(在VS调试时传递从XML文档对象复制到CreateAlbumFromXMLDoc法):

 < ;? XML版本=1.0编码=UTF-8>?; 
< photos_GetAlbums_response XSI:的schemaLocation =htt​​p://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd列表=真>
<专辑和GT;
<&援助GT; 3231990241086938677< /援助>
< cover_pid> 7031990241087042549< / cover_pid>
<所有者GT; 1337262814< /所有者>
<名称>&LA LT; /名称>
<制作而成的GT; 1233469624< /创建>
<变形> 1233469942&下; /修饰GT;
<描述>前往LA< /描述>
<地点> CA< /地点>
<链路GT; HTTP://www.example.com/album.php援助= 7333和ID = 1337262814< /链接>
<尺寸和GT; 48 LT; /尺寸与GT;
<可见的GT;朋友和LT; /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 7031990241086936240< /援助>
< cover_pid> 7031990241087005994< / cover_pid>
<所有者GT; 1337262814< /所有者>
<名称>长城照片< /名称>
<制作而成的GT; 1230437805< /创建>
<变形> 1233460690&下; /修饰GT;
<描述/>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 3296和ID = 1337262814< /链接>
<尺寸和GT; 34 - ; /尺寸与GT;
<可见的GT;人人< /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 7031990241086937544< /援助>
< cover_pid> 7031990241087026027< / cover_pid>
<所有者GT; 1337262814< /所有者>
<名称>从手机上传< /名称>
<制作而成的GT; 1231984989< /创建>
<变形> 1233460349&下; /修饰GT;
<描述/>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 6300和ID = 1337262814< /链接>
<&尺寸→3 LT; /尺寸与GT;
<可见的GT;朋友和LT; /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 7031990241086936188< /援助>
< cover_pid> 7031990241087005114< / cover_pid>
<所有者GT; 1337262814< /所有者>
<名称> 2008年圣诞节< /名称>
<制作而成的GT; 1230361978< /创建>
<变形> 1230362306&下; /修饰GT;
<描述>我的相册< /描述>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 5234和ID = 1337262814< /链接>
<尺寸和GT; 50℃/尺寸与GT;
<可见的GT;朋友和LT; /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 7031990241086935881< /援助>
< cover_pid> 7031990241087001093< / cover_pid>
<所有者GT; 1637262814< /所有者>
<名称>飞腓节< /名称>
<制作而成的GT; 1229889219< /创建>
<变形> 1229889235&下; /修饰GT;
<描述>其它照片管理< /描述>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 4937和ID = 1637262814< /链接>
<尺寸大于1< /尺寸与GT;
< /可见光>朋友-OF-朋友和LT;可见的GT;
< /专辑和GT;
<专辑和GT;
<&援助GT; 7031990241086935541< /援助>
< cover_pid> 7031990241086996817< / cover_pid>
<所有者GT; 1637262814< /所有者>
<名称>试验相册2(工作)LT; /名称>
<制作而成的GT; 1229460455< /创建>
<变形> 1229460475&下; /修饰GT;
<描述>这是一个测试专辑< /描述>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 4547和ID = 1637262814< /链接>
<尺寸大于1< /尺寸与GT;
<可见的GT;人人< /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 7031990241086935537< /援助>
< cover_pid> 7031990241086996795< / cover_pid>
<所有者GT; 1637262814< /所有者>
<名称>试验相册(工作)LT; /名称>
<制作而成的GT; 1229459168< /创建>
<变形> 1229459185&下; /修饰GT;
<描述>测试工作和LT; /描述>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 4493和ID = 1637262814< /链接>
<尺寸大于1< /尺寸与GT;
<可见的GT;朋友和LT; /可见光>
< /专辑和GT;
< / photos_GetAlbums_response>



一个侧面说明:只是为了它的地狱,我粘贴XML转换成XML记事本2007年,它告诉我:



您的XML文档不包含xml-stylesheet处理指令。为提供XSLT转换,以下内容添加到您的文件的顶部,并进行相应的编辑href属性:





我不认为这实际上意味着它的格式不正确或什么,但只是要注意的。



所以..



我的最终目标是让显然通过这该死的错误,并取回使用上面,一旦我能得到过去错误我的代码专辑的数组。我也想确保我的代码是在试图检索用我的相册类或其他任何东西我会在这里失去了我的相册[]属性arrray回唱片是正确的。我认为这是相当接近,应该工作,但事实并非如此。






跟进。我一直在拉我的头发至今。



下面是最新的。我不喜欢用枚举一些事情,现在(从马克),等等。后来我可能会改变。我也拿出了日期时间的东西,因为它只是看着奇怪,不,我没有得到上安韦错误......至少还没有。现在主要的问题依然是我那该死的XML。



它仍然出现能与我猜的格式问题?除非它是掩盖了另外一个问题,没有任何线索。这是推动我Fing头疯狂



我现在得到这个错误时,在反序列化踢:



 数据在根级别无效。 1号线,位置1 



在此行中我的​​代码发生错误:GetAlbumsResponse专辑=(GetAlbumsResponse )serializer.Deserialize(读卡器);



我如何得到的回应到一个XML文档

 公共静态的XmlDocument GetResponseXmlDocument(HttpWebResponse响应)
{
流数据流= NULL; //自WebResponse
XmlDocument的DOC =新的XmlDocument()流;

如果(文件== NULL)
{
抛出新的NullReferenceException(网络效应初探为空);
}

//获取响应流,所以我们可以读取响应$ B $的本体B数据流= response.GetResponseStream();

//使用容易获得
StreamReader的读者=新的StreamReader(数据流)一个StreamReader打开流;

//加载响应转换成字符串变量,这样我们就可以加载到一个XML文档
串responseString = reader.ReadToEnd();

//创建一个XML文档和放大器;响应数据
doc.LoadXml(responseString)加载它;

//表示响应
返回DOC最终的XML文档;
}



我的相册类放大器&;根级别类(得益于Marc..I帮助现在得到它):



 命名空间xxx.Entities 
{

[Serializable接口,XmlRoot(photos_GetAlbums_response)]
公共类GetAlbumsResponse
{
[的XmlElement(专辑)]
公开名单<相册和GT;相册{搞定;组; }

[XmlAttribute(名单)]
公共BOOL IsList {搞定;组; }
}

公共类专辑
{
#区域构造

公共专辑()
{

}

#endregion

#地区ElementConstants

///<总结>
///常量类杜绝使用魔法字符串(硬编码字符串)
///<的; /总结>
公共静态类ElementConstants
{
公共常量字符串援助=援助;
公共常量字符串所有者=所有者;
公共常量字符串ALBUMNAME =名;
公共常量字符串CoverPhotoID =cover_pid;
公共常量字符串CREATEDATE =创造;
公共常量字符串LastModifiedDate =修改;
公共常量字符串描述=说明;
公共常量字符串位置=地利;
公共常量字符串AlbumURL =联系;
公共常量字符串大小=大小;
公共常量字符串可见=看得见;
}

#endregion ElementConstants

#地区的公共属性

[的XmlElement(的ElementName = ElementConstants.aID,数据类型=字符串 )
公共字符串ALBUMID {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.CoverPhotoID,数据类型=INT)]
公众的Int32 CoverPhotoID {搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Owner,数据类型=字符串)]
公共字符串所有者获得{;组; }

[的XmlElement(的ElementName = ElementConstants.AlbumName,数据类型=字符串)]
公共字符串ALBUMNAME {搞定;组; }

公共字符串创建{搞定;组; }

公众的DateTime修改{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Description,数据类型=字符串)]
公共字符串描述{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Location,数据类型=字符串)]
公共字符串位置{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.AlbumURL,数据类型=字符串)]
公共字符串链接{搞定;组; }

公共字符串大小{搞定;组; }

[的XmlElement(的ElementName = ElementConstants.Visible,数据类型=字符串)]
公共字符串可见{搞定;组; }

#endregion
}
}



我的序列化器类:



 命名空间xxx.Utilities 
{
公共类串行
$ { b $ b酒店的公共静态列表<相册和GT; CreateAlbumFromXMLDoc(XmlDocument的DOC)
{
//创建一个串行
无功序列化的一个实例=新的XmlSerializer(typeof运算(专辑));
变种读者=新StringReader(doc.ToString());

//反序列化的XML对象,并转换为类型专辑
GetAlbumsResponse专辑=(GetAlbumsResponse)serializer.Deserialize(读卡器);

返回album.Albums;
}
}
}



真正的XML传入,即我想反序列化(是的,它确实具有的xmlns):



 < XML版本=1.0编码=UTF -8>?; 
< photos_GetAlbums_response的xmlns =htt​​p://api.example.com/1.0/的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instanceXSI:=的schemaLocationHTTP ://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd名单=真>
<专辑和GT;
<&援助GT; 7321990241086938677< /援助>
< cover_pid> 7031990241087042549< / cover_pid>
<所有者GT; 1124262814< /所有者>
<名称>相册测试1 LT; /名称>
<制作而成的GT; 1233469624< /创建>
<变形> 1233469942&下; /修饰GT;
<描述>我们的旅行< /描述>
<地点> CA< /地点>
<链路GT; HTTP://www.example.com/album.php援助= 7733和ID = 1124262814< /链接>
<尺寸和GT; 48 LT; /尺寸与GT;
<可见的GT;朋友和LT; /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 231990241086936240< /援助>
< cover_pid> 7042330241087005994< / cover_pid>
<所有者GT; 1124262814< /所有者>
<名称>相册测试2版; /名称>
<制作而成的GT; 1230437805< /创建>
<变形> 1233460690&下; /修饰GT;
<描述/>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 5296和ID = 1124262814< /链接>
<尺寸和GT; 34 - ; /尺寸与GT;
<可见的GT;人人< /可见光>
< /专辑和GT;
<专辑和GT;
<&援助GT; 70319423341086937544< /援助>
< cover_pid> 7032390241087026027< / cover_pid>
<所有者GT; 1124262814< /所有者>
<名称>相册测试3'; /名称>
<制作而成的GT; 1231984989< /创建>
<变形> 1233460349&下; /修饰GT;
<描述/>
<位置/>
<链路GT; HTTP://www.example.com/album.php援助= 6600和ID = 1124262814< /链接>
<&尺寸→3 LT; /尺寸与GT;
<可见的GT;朋友和LT; /可见光>
< /专辑和GT;
< / photos_GetAlbums_response>


解决方案

就个人而言,我不会在这里使用常量 - 它们使其难以发现错误(因为你很可能没有被重新使用它们,不要加太多)。例如:

  [XmlElement的(的ElementName = ElementConstants.AlbumName,数据类型=字符串)] 
公共字符串ALBUMID {搞定;组; }
...
[的XmlElement(的ElementName = ElementConstants.AlbumName,数据类型=字符串)]
公共字符串ALBUMNAME {搞定;组; }



看起来可疑,我...



这是比较简单的方法是将你想要的XML写入一个文件( foo.xml ,说),并使用:

  XSD foo.xml 
XSD foo.xsd /班

然后再看看 foo.cs


This is my first time using XML Serialization and this is driving me absolutely nuts after 2 days of trying to troubleshoot this.

I get this error when the deserialization kicks in:

The XML element 'name' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.

The error happens on this line in my code:

Album album = (Album)serializer.Deserialize(reader);

I not sure why. There is no dup "name" node so I just don't get it. This is an XML doc received from an HttpWebResponse from a 3rd party REST API.

Here's the complete code:

My Album Class (the type I'm Deserializing to):

    public class Album
    {
        #region Constructors

        public Album() 
        { 

        }

        #endregion

        #region ElementConstants

        public static class ElementConstants
        {
            public const string aID = "aid";
            public const string Owner = "owner";
            public const string AlbumName = "name";
            public const string CoverPhotoID = "cover_pid";
            public const string CreateDate = "created";
            public const string LastModifiedDate = "modified";
            public const string Description = "description";
            public const string Location = "location";
            public const string AlbumURL = "link";
            public const string Size = "size";
            public const string Visible = "visible";
        }

        #endregion ElementConstants

        #region Public Properties

        [XmlArray(ElementName = "photos_GetAlbums_response")]
        [XmlArrayItem( "album" )]
        public Album[] Albums { get; set; }

        [XmlElement (ElementName = ElementConstants.AlbumName, DataType = "string")]
        public string AlbumID { get; set; }

        [XmlElement(ElementName = ElementConstants.aID, DataType = "int")]
        public Int32 CoverPhotoID { get; set; }

        [XmlElement(ElementName = ElementConstants.Owner, DataType = "string")]
        public string Owner { get; set; }

        [XmlElement(ElementName = ElementConstants.AlbumName, DataType = "string")]
        public string AlbumName { get; set; }

        [XmlElement(ElementName = ElementConstants.aID, DataType = "DateTime")]
        public DateTime CreateDate { get; set; }

        [XmlElement(ElementName = ElementConstants.LastModifiedDate, DataType = "DateTime")]
        public DateTime LastModifiedDate { get; set; }

        [XmlElement(ElementName = ElementConstants.Description, DataType = "string")]
        public string Description { get; set; }

        [XmlElement(ElementName = ElementConstants.Location, DataType = "string")]
        public string Location { get; set; }

        [XmlElement(ElementName = ElementConstants.AlbumURL, DataType = "string")]
        public string Link { get; set; }

        [XmlElement(ElementName = ElementConstants.Size, DataType = "size")]
        public string Size { get; set; }

        [XmlElement(ElementName = ElementConstants.Visible, DataType = "string")]
        public string Visible { get; set; }

        #endregion
    }

My Serializer Class:

    public class Serializer
    {
        public static Album CreateAlbumFromXMLDoc(XmlDocument doc)
        {
            // Create an instance of a serializer
            var serializer = new XmlSerializer(typeof(Album));
            var reader = new StringReader(doc.ToString());

            // Deserialize the Xml Object and cast to type Album
            Album album = (Album)serializer.Deserialize(reader);

            return album;
        }
    }

The XML that I am trying to Deserialized (copied from the Xml Doc object being passed into the CreateAlbumFromXMLDoc method when debugging in VS):

<?xml version="1.0" encoding="UTF-8"?>
<photos_GetAlbums_response xsi:schemaLocation="http://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd" list="true">
<album>
 <aid>3231990241086938677</aid>
 <cover_pid>7031990241087042549</cover_pid>
 <owner>1337262814</owner>
 <name>LA</name>
 <created>1233469624</created>
 <modified>1233469942</modified>
 <description>trip to LA</description>
 <location>CA</location>
 <link>http://www.example.com/album.php?aid=7333&id=1337262814</link>
 <size>48</size>
 <visible>friends</visible>
 </album>
<album>
 <aid>7031990241086936240</aid>
 <cover_pid>7031990241087005994</cover_pid>
 <owner>1337262814</owner>
 <name>Wall Photos</name>
 <created>1230437805</created>
 <modified>1233460690</modified>
 <description/>
 <location/>
 <link>http://www.example.com/album.php?aid=3296&id=1337262814</link>
 <size>34</size>
 <visible>everyone</visible>
 </album>
<album>
 <aid>7031990241086937544</aid>
 <cover_pid>7031990241087026027</cover_pid>
 <owner>1337262814</owner>
 <name>Mobile Uploads</name>
 <created>1231984989</created>
 <modified>1233460349</modified>
 <description/>
 <location/>
 <link>http://www.example.com/album.php?aid=6300&id=1337262814</link>
 <size>3</size>
 <visible>friends</visible>
 </album>
<album>
 <aid>7031990241086936188</aid>
 <cover_pid>7031990241087005114</cover_pid>
 <owner>1337262814</owner>
 <name>Christmas 2008</name>
 <created>1230361978</created>
 <modified>1230362306</modified>
 <description>My Album</description>
 <location/>
 <link>http://www.example.com/album.php?aid=5234&id=1337262814</link>
 <size>50</size>
 <visible>friends</visible>
 </album>
<album>
 <aid>7031990241086935881</aid>
 <cover_pid>7031990241087001093</cover_pid>
 <owner>1637262814</owner>
 <name>Hock</name>
 <created>1229889219</created>
 <modified>1229889235</modified>
 <description>Misc Pics</description>
 <location/>
 <link>http://www.example.com/album.php?aid=4937&id=1637262814</link>
 <size>1</size>
 <visible>friends-of-friends</visible>
 </album>
<album>
 <aid>7031990241086935541</aid>
 <cover_pid>7031990241086996817</cover_pid>
 <owner>1637262814</owner>
 <name>Test Album 2 (for work)</name>
 <created>1229460455</created>
 <modified>1229460475</modified>
 <description>this is a test album</description>
 <location/>
 <link>http://www.example.com/album.php?aid=4547&id=1637262814</link>
 <size>1</size>
 <visible>everyone</visible>
 </album>
<album>
 <aid>7031990241086935537</aid>
 <cover_pid>7031990241086996795</cover_pid>
 <owner>1637262814</owner>
 <name>Test Album (for work)</name>
 <created>1229459168</created>
 <modified>1229459185</modified>
 <description>Testing for work</description>
 <location/>
 <link>http://www.example.com/album.php?aid=4493&id=1637262814</link>
 <size>1</size>
 <visible>friends</visible>
 </album>
 </photos_GetAlbums_response>

A side note: Just for the hell of it, I paste that XML into XML Notepad 2007, it tells me:

Your XML document contains no xml-stylesheet processing instruction. To provide an XSLT transform, add the following to the top of your file and edit the href attribute accordingly:

I don't think that really means it's malformed or anything but just something to note.

So..

My ultimate goal is to get pass this damn error obviously and get an array of albums back using my code above once I can get past the error. I also want to make sure my code is correct in trying to retrieve that arrray back of albums using my Album[] property in my Album class or anything else I might be missing here. I think it's pretty close and should work but it's not.


Follow-up. I've been pulling my hair out since then.

Here's the latest. I did not use some things for now (from Marc) like the Enum, etc. I might change that later. I also pulled out the datetime stuff as it just looked wierd and I did not get errors on that anway without...at least yet. The main problem now is still my damn XML.

It's still appearing to have problems with the format I guess? Unless it's covering up another problem, no clue. This is driving me fing crazy.

I now get this error when the deserialization kicks in:

Data at the root level is invalid. Line 1, position 1.

The error happens on this line in my code: GetAlbumsResponse album = (GetAlbumsResponse)serializer.Deserialize(reader);

How I get the response into an XmL doc:

public static XmlDocument GetResponseXmlDocument(HttpWebResponse response)
        {
            Stream dataStream = null; // stream from WebResponse
            XmlDocument doc = new XmlDocument();

            if (doc == null)
            {
                throw new NullReferenceException("The web reponse was null");
            }

            // Get the response stream so we can read the body of the response
            dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access
            StreamReader reader = new StreamReader(dataStream);

            // Load response into string variable so that we can then load into an XML doc
            string responseString = reader.ReadToEnd();

            // Create an XML document & load it with the response data
            doc.LoadXml(responseString);

            // Final XML document that represents the response
            return doc;
        }

My Album Class & Root Level Class (thanks to help from Marc..I get it now):

namespace xxx.Entities
{

    [Serializable, XmlRoot("photos_GetAlbums_response")]
    public class GetAlbumsResponse
    {
        [XmlElement("album")]
        public List<Album> Albums { get; set; }

        [XmlAttribute("list")]
        public bool IsList { get; set; }
    }

    public class Album
    {
        #region Constructors

        public Album()
        {

        }

        #endregion

        #region ElementConstants

        /// <summary>
        /// Constants Class to eliminate use of Magic Strings (hard coded strings)
        /// </summary>
        public static class ElementConstants
        {
            public const string aID = "aid";
            public const string Owner = "owner";
            public const string AlbumName = "name";
            public const string CoverPhotoID = "cover_pid";
            public const string CreateDate = "created";
            public const string LastModifiedDate = "modified";
            public const string Description = "description";
            public const string Location = "location";
            public const string AlbumURL = "link";
            public const string Size = "size";
            public const string Visible = "visible";
        }

        #endregion ElementConstants

        #region Public Properties

        [XmlElement (ElementName = ElementConstants.aID, DataType = "string")]
        public string AlbumID { get; set; }

        [XmlElement(ElementName = ElementConstants.CoverPhotoID, DataType = "int")]
        public Int32 CoverPhotoID { get; set; }

        [XmlElement(ElementName = ElementConstants.Owner, DataType = "string")]
        public string Owner { get; set; }

        [XmlElement(ElementName = ElementConstants.AlbumName, DataType = "string")]
        public string AlbumName { get; set; }

        public string Created { get; set; }

        public DateTime Modified { get; set; }

        [XmlElement(ElementName = ElementConstants.Description, DataType = "string")]
        public string Description { get; set; }

        [XmlElement(ElementName = ElementConstants.Location, DataType = "string")]
        public string Location { get; set; }

        [XmlElement(ElementName = ElementConstants.AlbumURL, DataType = "string")]
        public string Link { get; set; }

        public string Size { get; set; }

        [XmlElement(ElementName = ElementConstants.Visible, DataType = "string")]
        public string Visible { get; set; }

        #endregion
    }
}

My Serializer Class:

namespace xxx.Utilities
{
    public class Serializer
    {
        public static List<Album> CreateAlbumFromXMLDoc(XmlDocument doc)
        {
            // Create an instance of a serializer
            var serializer = new XmlSerializer(typeof(Album));
            var reader = new StringReader(doc.ToString());

            // Deserialize the Xml Object and cast to type Album
            GetAlbumsResponse album = (GetAlbumsResponse)serializer.Deserialize(reader);

            return album.Albums;
        }
    }
}

The true XML incoming, that I am trying to Deserialize (yes it does have xmlns):

<?xml version="1.0" encoding="UTF-8"?>
<photos_GetAlbums_response xmlns="http://api.example.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd" list="true">
  <album>
    <aid>7321990241086938677</aid>
    <cover_pid>7031990241087042549</cover_pid>
    <owner>1124262814</owner>
    <name>Album Test 1</name>
    <created>1233469624</created>
    <modified>1233469942</modified>
    <description>Our trip</description>
    <location>CA</location>
    <link>http://www.example.com/album.php?aid=7733&id=1124262814</link>
    <size>48</size>
    <visible>friends</visible>
  </album>
  <album>
    <aid>231990241086936240</aid>
    <cover_pid>7042330241087005994</cover_pid>
    <owner>1124262814</owner>
    <name>Album Test 2</name>
    <created>1230437805</created>
    <modified>1233460690</modified>
    <description />
    <location />
    <link>http://www.example.com/album.php?aid=5296&id=1124262814</link>
    <size>34</size>
    <visible>everyone</visible>
  </album>
  <album>
    <aid>70319423341086937544</aid>
    <cover_pid>7032390241087026027</cover_pid>
    <owner>1124262814</owner>
    <name>Album Test 3</name>
    <created>1231984989</created>
    <modified>1233460349</modified>
    <description />
    <location />
    <link>http://www.example.com/album.php?aid=6600&id=1124262814</link>
    <size>3</size>
    <visible>friends</visible>
  </album>
</photos_GetAlbums_response>

解决方案

Personally, I wouldn't use constants here - they make it hard to spot errors (and since you probably aren't re-using them, don't add much). For example:

    [XmlElement (ElementName = ElementConstants.AlbumName, DataType = "string")]
    public string AlbumID { get; set; }
...
    [XmlElement(ElementName = ElementConstants.AlbumName, DataType = "string")]
    public string AlbumName { get; set; }

Looks suspect to me...

An easier approach is to write the xml you want to a file (foo.xml, say) and use:

xsd foo.xml
xsd foo.xsd /classes

Then look at foo.cs.

这篇关于反序列化错误:从命名空间'的XML元素'名'已经存在于当前范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆