且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正确的XML序列化和&QUOT反序列化;混合"在.NET类型

更新时间:2022-02-12 08:35:31

要解决这个问题,我不得不修改生成的类:

To solve this problem I had to modify the generated classes:

  1. 将在 XmlTextAttribute 文本属性设置为产品属性并添加参数键入= typeof运算(字符串)
  2. 删除文本属性
  3. 删除 textField的字段
  1. Move the XmlTextAttribute from the Text property to the Items property and add the parameter Type = typeof(string)
  2. Remove the Text property
  3. Remove the textField field

结果在生成code(修改)是这样的:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName="StrucDoc.Paragraph", Namespace="urn:hl7-org:v3")]
public partial class StrucDocParagraph {

    private StrucDocCaption captionField;

    private object[] itemsField;

    private string idField;

    // ...fields for other attributes...

    /// <remarks/>
    public StrucDocCaption caption {
        get {
            return this.captionField;
        }
        set {
            this.captionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("br", typeof(StrucDocBr))]
    [System.Xml.Serialization.XmlElementAttribute("sub", typeof(StrucDocSub))]
    [System.Xml.Serialization.XmlElementAttribute("sup", typeof(StrucDocSup))]
    // ...other possible nodes...
    [System.Xml.Serialization.XmlTextAttribute(typeof(string))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="ID")]
    public string ID {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    // ...properties for other attributes...
}

现在,如果我的反序列化 XML元素所在的段落节点是这样的:

Now if I deserialize an XML element where the paragraph node looks like this:

<paragraph>first line<br /><br />third line</paragraph>

结果是该项目的数组是这样写的:

The result is that the item array is read like this:

itemsField = new object[]
{
    "first line",
    new StrucDocBr(),
    new StrucDocBr(),
    "third line",
};

这是正是我需要的项,顺序和内容是正确
如果我的连载这一次,结果又是正确的:

This is exactly what I need, the order of the items and their content is correct.
And if I serialize this again, the result is again correct:

<paragraph>first line<br /><br />third line</paragraph>

什么我指出了正确的方向是由纪尧姆答案,我还以为是什么可能是这样。然后有这个在MSDN文档 XmlTextAttribute

您可以应用在 XmlTextAttribute 以   返回一个字段或属性   字符串数组。 您也可以应用   的属性类型的数组   对象,但你必须设置类型   属性字符串。在这种情况下,任何   插入到字符串数组是   序列化为XML文本。

You can apply the XmlTextAttribute to a field or property that returns an array of strings. You can also apply the attribute to an array of type Object but you must set the Type property to string. In that case, any strings inserted into the array are serialized as XML text.

因此​​,序列化和反序列化工作正确的,但我不知道是否有其他的副作用。也许这是不可能生成这些类与XSD.EXE一个模式了,但我不需要反正。

So the serialization and deserialization work correct now, but I don't know if there are any other side effects. Maybe it's not possible to generate a schema from these classes with xsd.exe anymore, but I don't need that anyway.