0

我是使用 xml 模式的新手。

我设法使用 xsd.exe 为我的架构创建了一个类,并且我设法生成了以下代码。

static void Main(string[] args)
{
    var MarketDeclaration_detail = new TransactionsPalletsPalletMarketDeclaration { Set = "", Declaration = "" };
    var MarketDeclaration_class = new TransactionsPalletsPalletMarketDeclaration[] { MarketDeclaration_detail };

    var PalletLine_detail = new TransactionsPalletsPalletPalletLine { ProductLineNo = "", Quantity = "" };
    var PalletLine_class = new TransactionsPalletsPalletPalletLine[] { PalletLine_detail };

    var pallet_detail = new TransactionsPalletsPallet { AccreditDate = "", Available = "", Customer = "", MarketDeclaration = MarketDeclaration_class, MixedPltIndicator = "",
                                                        PalletLine = PalletLine_class, PalletNo = "", PalletType = "", PartPltIndicator = "", PltSubmisProfile = "", SLOC = "", Status = "", StorageType = "",
                                                        Trader = "", TransferInNo = "", TransferOutNo = "", TransferOutOrder = "", TransferOutOrderLine = ""};

    var pallet_class = new TransactionsPalletsPallet[] { pallet_detail };

    var Materials_detail = new TransactionsReferencesMaterialsMaterial { MaterialNo = "", Trader = "", Brand = "", Variety = "", Size = "", Pack = "", GrowingMethod = "", Grade = "", PackTreatment = "", MarketAttribute = "", Count =""};
    var Materials_class = new TransactionsReferencesMaterialsMaterial[] { Materials_detail };

    var ProductLines_detail = new TransactionsReferencesProductLinesProductLine { ColourBand = "", Customer = "", ExpiryDate = "", Grower = "", HarvestDate = "", MaterialNo = "", MgmtArea = "", Owner = "", 
                                                                                  PackDate = "", Packer = "", PackWeight = "", ProblemRisk = "", ProductionProgram = "", ProductLineNo = "", RPIN = "", Run = "", 
                                                                                  SGContract = "", SGContractLine = "", Subdivision = "", SubmisProfile = "", SupplyGroup = "", Trader = ""};
    var ProductLines_class = new TransactionsReferencesProductLinesProductLine[] { ProductLines_detail };

    var TransfersOut_detail = new TransactionsReferencesTransfersOutTransfer { CarrierID = "", ContainerNo = "", DestinationPort = "", DischargePort = "", DispatchDate = "", DispatchTime = "", ETA = "", From = "", LoadPort = "", ReleaseNo = "",
                                                                               SealNo = "", Temperature1 = "", Temperature2 = "", TempLogger = "", To = "", TransferNo = "", TransferRef = "", TruckID = "", Vessel = "", Voyage = ""};
    // var TransfersOut_class = new TransactionsReferencesTransfersOutTransfer[] { TransfersOut_detail };
    TransfersOut();

    var references_detail = new TransactionsReferences { Materials = Materials_class, ProductLines = ProductLines_class, TransfersOut = TransfersOut_class };
    var references_class = new TransactionsReferences[] { references_detail };

    var data1 = new Transactions { Pallets = pallet_class, References = references_class, Serialnumber = "", Timestamp = "" };

    var serializer1 = new XmlSerializer(typeof(Transactions));

    using (var stream1 = new StreamWriter("C:\\Dave\\xml\\djb2.xml"))
        serializer1.Serialize(stream1,data1);
}

public static TransactionsReferencesTransfersOutTransfer[] TransfersOut_class;

public static void TransfersOut()
{
    TransactionsReferencesTransfersOutTransfer[] TransfersOut_detail1 = new TransactionsReferencesTransfersOutTransfer[3];            

    for (int i = 0; i < 3; i++)
    {
       TransfersOut_detail1[i] = new TransactionsReferencesTransfersOutTransfer { CarrierID = Convert.ToString(i) };

       TransfersOut_class = new TransactionsReferencesTransfersOutTransfer[] { TransfersOut_detail1[0], TransfersOut_detail1[1], TransfersOut_detail1[2] };
    }
}

一切正常。

按问题/问题是

使用我的 TransfersOut 方法,我将 for 循环限制为 3,就像使用 TransferOut_details 一样。但是,在从数据库返回数据之前,我不知道会有多少。3被用作测试。

我不确定如何对此进行编码,因此我会将所有行返回/写入 XML 文件。

任何想法/帮助将不胜感激。

我注意到我是否以最好的方式对此进行了编码。

4

1 回答 1

0

好的。我弄错了,并且已经解决了这个问题,但是这产生了一个我正在尝试解决的新问题,这可能会产生一个关于未设置为对象实例的对象引用的单独问题。

于 2013-05-30T04:05:10.267 回答