c# 序列化

编程入门 行业动态 更新时间:2024-10-26 02:26:53
本文介绍了c# 序列化 - 包含具有属性的简单内容的复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个现有的解决方案,它使用 svcutil 创建了一组基于 XSD 架构的类.我现在不得不对该架构进行编辑,但我遇到了一些绊脚石.

I have an existing solution that has used svcutil to create a set of classes based on an XSD schema. I am now having to make edits to that schema and I have run into a bit of a stumbling block.

架构将使用如下类型进行扩展:

The schema is going to be extended with types like this:

<xsd:complexType name="Awkward"> <xsd:sequence> <xsd:element name="awkward1" type="tt:AwkwardChild" nillable="true"> </xsd:element> <xsd:element name="awkward2" type="tt:AwkwardChild" nillable="true"> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="AwkwardChild"> <xsd:simpleContent> <xsd:extension base="tt:AwkwardChildType"> <xsd:attribute name="id" type="xsd:ID"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:simpleType name="AwkwardChildType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Dependent"/> <xsd:enumeration value="In between"/> <xsd:enumeration value="Independent"/> </xsd:restriction> </xsd:simpleType>

这会导致 XML 输出如下:

Which would result in XML output something like this:

<Awkward> <awkward1 id="z1">Dependent</awkward1> <awkward2 id="z2">Independent</awkward2> </Awkward>

SVCUtil 在尝试生成此抱怨时窒息

SVCUtil is choking when trying to generate this complaining that

无法导入命名空间 tt 中的类型 'AwkwardChild'.不支持具有简单内容扩展的复杂类型.更改架构以便类型可以映射到数据协定类型,或者使用 ImporXmlType 或使用不同的序列化程序."

"Type 'AwkwardChild' in namespace tt cannot be imported. Complex types with simple content extension are not supported. Either change the schema so that the types can map to data contract types or use ImporXmlType or use a different serializer."

我想我明白这一点,因为它试图输出一个字符串类型,但在其中包含属性.

And I think that I understand this, as it is trying to output a string type, but include attributes with it.

我试图弄清楚是否有一种方法可以手动编写一个类来实现这种输出,但是我想不出一种方法可以让字符串在 xml 中显示为简单内容"节点而不是作为子元素,例如这个类:

I am trying to figure out if there is a way I can hand code a class to achieve this kind of output, but I can't figure out a way to get the string to appear as 'simple content' in the xml node rather than as a child element, e.g. this class:

[DataContractAttribute(Name = "AwkwardChild", Namespace = "tt")] public class Awkward { [DataContractAttribute(Name="id")] public string id { get; set; } //What do I put here to get this to appear as the content of //the awkward node, not in an element? public string nodecontent { get; set; } }

有人能指出我正确的方向吗?

Could somebody point me in the right direction?

推荐答案

使用 SVCUtil 工具和 XMLSerializer 选项无法解决此问题.最后,我必须为此特定部分手动编写一个类,该类实现 IXmlSerializable 并在 write XML 方法中输出适当的 XML.下面的小示例片段.

This could not be worked around using the SVCUtil tool and the XMLSerializer option. In the end, I had to hand code a class for this particular section that implemented IXmlSerializable and outputted the appropriate XML in the write XML method. Small sample snippet below.

writer.WriteStartElement("awkward1"); writer.WriteAttributeString("id1", "z1"); writer.WriteValue(nodeValue); writer.WriteEndElement();

更多推荐

c# 序列化

本文发布于:2023-11-16 16:01:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!