WCF/MVC3中的XML序列化 [英] XML Serialization in WCF/MVC3

查看:58
本文介绍了WCF/MVC3中的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用MVC3开发WCF应用程序.

我正在使用xmlserialization来序列化数据.

这是示例代码:

Hi,

I am developing WCF application using MVC3.

I m using xmlserialization to serialize data.

Here is the sample code:

namespace A{
[serializable]
[DataContract(Name="User")]
[XmlAttribute("User")]
Public class User { 

[DataMember(Name="ID"]
[XmlAttribute("ID"),IsRequired=false]
public void ID {get; set;}

[DataMember(Name="ID"]
[XmlAttribute("IDSpecified"),IsRequired=false]
public void IDSpecified{get; set;}

}
}


现在,我为该属性分配值,并尝试序列化XML.


Now I am assigning values to this properties and trying to serialize XML.

User.ID=1;
User.IDSpecified=2;


由于属性名称以相同的字符串"ID"开头,因此出现错误.如果我更改"Specified"而不是"IDSpecified",则可以进行序列化,因为属性名称不同.

因此,是否有任何规则规定属性名称不应以相同的字符开头.


请尽快帮助我.

谢谢.


It is giving error because property names are starting with same string "ID". If I change "Specified" instead of "IDSpecified" then it is serialize fine because property name is different.

So is there any rule that property name should not start with same characters.


Pls help me ASAP.

Thank you.

推荐答案

错误不是因为您有两个以"Id"开头的属性.这是因为两个属性的DataMember属性名称相同.将属性更改为下面的属性应该可以解决重复的"ID"错误.

The error is not because you have two properties that begin with "Id". It is because both properties have the same name for the DataMember Attribute. Changing the attribute to what is below should fix your duplicate "ID" error.

[DataMember(Name="IDSpecified"]
[XmlAttribute("IDSpecified"),IsRequired=false]
public void IDSpecified{get; set;}


hI,

对不起,有问题的错误.数据成员名称只是不同.

[DataMember(Name ="ID"]
[XmlAttribute("ID"),IsRequired = false]
公共无效ID {get;设置;}

[DataMember(Name ="SpecifiedID"]
[XmlAttribute("SpecifiedID"),IsRequired = false]
public void SpecifiedID {get;设置;}

我仍然遇到相同的错误.
hI,

Sorry for the mistake in question. Datamember names are different only.

[DataMember(Name="ID"]
[XmlAttribute("ID"),IsRequired=false]
public void ID {get; set;}

[DataMember(Name="SpecifiedID"]
[XmlAttribute("SpecifiedID"),IsRequired=false]
public void SpecifiedID{get; set;}

Still i m getting same error.


感谢您的回复.
我找到了解决方案.这是:
http://msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlserializer.aspx
thanks for the reply.
I found the solution. Here it is :
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx


这篇关于WCF/MVC3中的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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