刚接触c#,不知道怎么用(System.Xml.Serialization.XmlTypeAttribute)

春天到了冬天还远吗 2011-06-24 12:20:38
现在想用C#做个简单的测试网页
用vs带的wsdl生成一个cs文件不知道怎么用了

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.egeyes.cn/CMS/")]
public partial class CReq {

private object itemField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("C1", typeof(CC1), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("C2", typeof(CC2), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("C3", typeof(CC3), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("C4", typeof(CC4), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}


我猜测要从C1-C4中定义一个对象,但不知道怎么把这个对象跟Item 联系起来

比如
CReq req = new CReq();
C1 c = new C1();

我该怎么才能让C1通过Item和req联系起来?代码怎么写?
我没C#基础,只是暂时用下,希望大家能帮下忙
谢谢
...全文
369 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cento123 2011-06-24
  • 打赏
  • 举报
回复
C1 c;
// Construct an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer =
new XmlSerializer(typeof(C1));
// To read the file, create a FileStream.
FileStream myFileStream =
new FileStream("myFileName.xml", FileMode.Open);
// Call the Deserialize method and cast to the object type.
c = (C1)mySerializer.Deserialize(myFileStream)

myFileName.xml中每个xml节点是C1的一个属性。
Dobzhansky 2011-06-24
  • 打赏
  • 举报
回复
这个 item 可以是那四种类型的对象
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wtx_sonery 的回复:]

刚群里大牛指点了下
CReq req = new CReq();
C1 c = new C1();
req.Item = c;
报“System.NullReferenceException: 未将对象引用设置到对象的实例”错误
[/Quote]
是我自己写错了~~
  • 打赏
  • 举报
回复
刚群里大牛指点了下
CReq req = new CReq();
C1 c = new C1();
req.Item = c;
报“System.NullReferenceException: 未将对象引用设置到对象的实例”错误
  • 打赏
  • 举报
回复

不懂,帮顶.

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧