【发布时间】:2013-05-29 05:41:37
【问题描述】:
我不熟悉使用 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 文件。
任何想法/帮助将不胜感激。
我注意到我是否以最好的方式编写了代码。
【问题讨论】:
-
如果不知道您将在哪里/如何读取您的数据,我们不可能回答。如果它来自数据库,您需要代码来连接到该数据库并读取相关表 - 一旦您这样做了,要创建的对象数将自动来自您检索的行数......
-
是的,它将来自数据库。是的,我知道这会给我替换我的测试 3 的号码。我不知道怎么做
-
替换这一行,我也知道应该在for循环之外 TransfersOut_class= new TransactionsReferencesTransfersOutTransfer[] { TransfersOut_detail1[0], TransfersOut_detail1[1], TransfersOut_detail1[2] };一行代码将传递所有创建的对象
标签: c# xml xml-serialization xsd