如何使用XSD生成的c#类来序列化/反序列化XML的各个元素

本文关键字:反序列化 XML 元素 序列化 XSD 何使用 | 更新日期: 2023-09-27 18:09:34

我目前有一个从XML模式生成的c#类,它用于获取XML文件并更新数据库中的值。

我卡住的地方是这些值中的一个(由于复杂性和可变性)需要作为独立的xml存储在数据库中,然后我将需要在运行时反序列化。

是否可以定义第二个c#类来处理这个元素而不干扰主类?

或者在重新序列化以保存时更改该节点的名称会更简单吗?

编辑:我为缺乏上下文道歉,很晚了,我正要出门。XML文件用于为不同的客户端设置web表单验证和定制,至少80%的模式是非常简单的数据(使字段成为强制性的,应用正则表达式,隐藏和显示字段是一些例子)

我提到的复杂部分与多个字段之间的条件验证有关。下面是XML的样例:

<?xml version="1.0"?>

<Relationships>
    <Relationship xsi:type="MutuallyExclusiveRelationship">
        <Fields>
            <Field Id="lineItemAfeNumber" IsInGrid="true"/>
            <Field Id="lineItemCostCenter" IsInGrid="true"/>
        </Fields>
    </Relationship>     
</Relationships>
<Fields>
    <Field xsi:type="TextField" Id="invoiceNumber">
        <ValidationRegex Value="^[0-9a-zA-Z'-]*$"/>
        <ValidationRegexMessage Value="{0} must be alpha-numeric and can contain dashes."/>
        <MaxLength Value="20"/>
    </Field>
    <Field xsi:type="TextField" Id="afeNumber">
        <InputMask Value="aa999999"/>
        <ValidationRegex Value="^[A-Za-z]{2}[0-9]{6}$"/>
        <ValidationRegexMessage Value="{0} must be in the format AA999999."/>
    </Field>
    <Field xsi:type="TextField" Id="costCenter">
        <ValidationRegex Value="^[a-zA-Z0-9]*$"/>
        <ValidationRegexMessage Value="{0} must be alpha-numeric."/>
        <MinLength Value="8"/>
        <MaxLength Value="9"/>
    </Field>

    <Field xsi:type="TextField" Id="orderNumber">
        <MinLength Value="1"/>
        <MaxLength Value="12"/>
    </Field>
    <Field xsi:type="TextField" Id="generalLedgerCode">
        <InputMask Value="9999.999"/>
        <ValidationRegex Value="^[0-9]{4}'.[0-9]{3}$"/>
        <ValidationRegexMessage Value="{0} must be in the format 0000.000."/>
    </Field>
    <Field xsi:type="TextField" Id="approverId">
        <Label Value="Approver Code"/>
        <MaxLength Value="10"/>
    </Field>
    <Field xsi:type="TextField" Id="leaseWell">
        <Label Value="Location/UWI"/>
    </Field>
    <Field xsi:type="TextField" Id="poNumber">
        <ValidationRegex Value="^[a-zA-Z0-9/-]*$"/>
        <ValidationRegexMessage Value="{0} must be alpha-numeric and can contain '-'."/>
        <MaxLength Value="12"/>
    </Field>
    <Field xsi:type="DropDownField" Id="currency">
        <Label Value="Currency"/>
        <DefaultValue Value="CAD"/>
        <Values>
            <DropDownValue Value="USD"/>
            <DropDownValue Value="CAD"/>
        </Values>
    </Field>    
    <Field xsi:type="TextField" Id="remitToTax">
        <Label Value="GST/HST #"/>
    </Field>
    <Field xsi:type="TextField" Id="detailsComment">
        <Mandatory Value="false"/>
        <MaxLength Value="40"/>
    </Field>    
    <Field xsi:type="TextField" Id="newComment">
        <MaxLength Value="40"/>
    </Field>
    <!-- Attachments -->
    <Field xsi:type="TextField" Id="attachmentFileName">
        <MandatoryMessage Value="Attachments are required."/>
    </Field>
    <Field xsi:type="DropDownField" Id="approverCompanyCode">
        <Mandatory Value="true"/>
    </Field>
    <Field xsi:type="TextField" Id="recipientName">
      <Mandatory Value="true"/>
    </Field>        
</Fields>
<Grids>
    <Grid Id="invoiceDetailsTable">
        <Fields>
            <Field xsi:type="TextField" Id="lineItemDescription">
                <MaxLength Value="40"/>
            </Field>                
            <Field xsi:type="TextField" Id="lineItemAfeNumber">
                <InputMask Value="aa999999"/>
                <ValidationRegex Value="^[A-Za-z]{2}[0-9]{6}$"/>
                <ValidationRegexMessage Value="{0} must be in the format AA999999."/>
            </Field>
            <Field xsi:type="TextField" Id="lineItemCostCenter">
                <ValidationRegex Value="^[a-zA-Z0-9]*$"/>
                <ValidationRegexMessage Value="{0} must be alpha-numeric."/>
                <MinLength Value="8"/>
                <MaxLength Value="9"/>
            </Field>
            <Field xsi:type="TextField" Id="lineItemOrderNumber">
                <MinLength Value="1"/>
                <MaxLength Value="12"/>
            </Field>
            <Field xsi:type="TextField" Id="lineItemLeaseWell">
                <Label Value="Location/UWI"/>
                <Mandatory Value="true"/>
            </Field>
            <Field xsi:type="TextField" Id="lineItemGlAccount">
                <InputMask Value="9999.999"/>
                <ValidationRegex Value="^[0-9]{4}'.[0-9]{3}$"/>
                <ValidationRegexMessage Value="{0} must be in the format 0000.000."/>
            </Field>

            <Field xsi:type="TextField" Id="lineItemPoNumber">
                <ValidationRegex Value="^[a-zA-Z0-9/-]*$"/>
                <ValidationRegexMessage Value="{0} must be alpha-numeric and can contain '-'."/>
                <MaxLength Value="12"/>
            </Field>
        </Fields>
    </Grid>
</Grids>

这里的关系部分是我想要重新序列化的部分,这显然是一个简单的例子,说明该部分可以是什么,还有更多的子元素没有显示。

如何使用XSD生成的c#类来序列化/反序列化XML的各个元素

当你保存主类到数据库时,为什么不序列化这个复杂的元素呢?例如,如果你的类是这样的:

public class Master
{
    public string simple;
    public Complex complex;
}

你可以用:

将复杂的元素保存为xml
void SaveToDB(Stream file)
{
    XmlSerializer masterSerializer = new XmlSerializer(typeof(Master));
    Master m = (Master)masterSerializer.Deserialize(file);
    //save m.simple to db
    XmlSerializer complexSerializer = new XmlSerializer(typeof(Complex));
    StringWriter complexXmlWriter = new StringWriter();
    complexSerializer.Serialize(complexXmlWriter, m.complex);
    string complexXml = complexXmlWriter.ToString();
    //save complexXml to db
}

这样你不需要改变你的主类,你可以保存复杂的元素在xml

经过进一步的调查和头脑风暴,我们确定这两个不同的序列化类实际上可以存在于不同的作用域,从而消除了我遇到的冲突。

相关文章: