c# 序列化 - 包含具有属性的简单内容的复杂类型 [英] c# serialization - complex type containing simple content with attributes

查看:45
本文介绍了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# 序列化 - 包含具有属性的简单内容的复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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