【发布时间】:2011-03-23 18:03:48
【问题描述】:
我尝试使用 XmlSerializer 进行序列化 - 但派生类有问题:
这些是我的课程:
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Child))]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.demo.com")]
public class BaseClass {
private int myIntField;
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int myInt {
get { return this.myIntField; }
set { this.myIntField = value; }
}
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.demo.com")]
public class Child : BaseClass {
private int keyField;
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int key {
get { return this.keyField; }
set { this.keyField = value; }
}
}
发现,只有在包含命名空间的情况下,父字段的序列化才有效:
XmlSerializer mySerializer = new XmlSerializer(typeof(Child));
// NOT WORKING
StringReader sr = new StringReader(
"<Child>" +
"<myInt>10</myInt>" +
"<key>1</key>" +
"</Child>");
/* WORKING
StringReader sr = new StringReader(
"<Child>" +
"<myInt xmlns=\"http://www.demo.com\">10</myInt>" +
"<key>1</key>" +
"</Child>");*/
Child myChild = (Child)mySerializer.Deserialize(sr);
在工作站框架上,不需要命名空间。
所以我在使用 web 服务时遇到了问题,因为命名空间不包含在父字段中。
【问题讨论】:
-
您确定是 3.5 版吗?我不认为那个版本存在。有 3/3.1 和 4/4.5。我从 2--1 的版本 5 开始只使用 CF,所以如果它真的是 3.x 版本,它可能不支持现代 Web 服务标准。
-
你没有使用 ColdFusion,所以你不应该包含 Coldfusion 标签。 ColdFusion != 紧凑的框架。看起来这是您在此问题上的第二篇文章(第一篇:stackoverflow.com/questions/5402678/…),而且您两次都错误地标记了它。
-
哦,我明白了-谢谢您的建议
-
请显示代码。另外,这是一个带有 [WebMethod] 的 [WebService] 吗?
-
这和stackoverflow.com/questions/5402678/…中的问题一样吗?
标签: .net compact-framework xml-serialization xmlserializer