1

現在、XML スキーマから生成した C# クラスがあります。これは、XML ファイルを取得してデータベース内の値を更新するために使用されます。

私が立ち往生しているのは、(複雑さと可変性のため) これらの値の 1 つをスタンドアロンの xml としてデータベースに格納する必要があり、実行時に逆シリアル化する必要があることです。

マスター クラスに干渉することなく、この 1 つの要素を処理する 2 番目の 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>

ここの関係セクションは、再シリアル化したい部分です。これは明らかに、セクションが何であるかを示す簡単な例です。表示されていない子要素がさらに多くあります。

4

2 に答える 2

0

マスタークラスをデータベースに保存するときに、この複雑な要素をシリアル化しないのはなぜですか? たとえば、クラスが次のような場合:

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

次のようにして、複雑な要素を xml として db に保存できます。

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 に保存できます。

于 2013-04-16T03:16:50.143 に答える