xml 序列化器,不要序列化 top class?
xml serializer, don't serialize top class?
我已经编写了序列化 class 的代码。一切正常,但我不想让我的 top class 包含在序列化中,最好的方法是什么?
示例 classes:
[XmlRoot(elementName: "ArrayOfDebtor")]
public class TrustIt
{
[XmlArray]
public List<Debtor> ArrayOfDebtor { get; set; }
}
public class Debtor
{
[XmlAttribute]
public string ImportCode { get; set; }
[XmlAttribute]
public string LanguageCode { get; set; }
[XmlAttribute]
public string CountryCode { get; set; }
[XmlElement]
public string Number { get; set; }
[XmlElement]
public string ParentDebtorNumber { get; set; }
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string AdditionalName { get; set; }
[XmlElement]
public string AddressLine1 { get; set; }
[XmlElement]
public string AddressLine2 { get; set; }
[XmlElement]
public string AddressLine3 { get; set; }
[XmlElement]
public string PostalCode { get; set; }
[XmlElement]
public string City { get; set; }
[XmlElement]
public string Telephone { get; set; }
[XmlElement]
public string Mobile { get; set; }
[XmlElement]
public string Fax { get; set; }
[XmlElement]
public string Email { get; set; }
[XmlElement]
public string CustomEmail { get; set; }
[XmlElement]
public string AccountManagerEmail { get; set; }
[XmlElement]
public string InternalEmail { get; set; }
[XmlElement]
public string Contact { get; set; }
[XmlElement]
public string InternalCreditLimit { get; set; }
[XmlElement]
public string ExternalCreditLimit { get; set; }
[XmlElement]
public string Category { get; set; }
[XmlElement]
public string Accountmanager { get; set; }
[XmlElement]
public string VATNumber { get; set; }
[XmlElement]
public string PaymentCondition { get; set; }
[XmlElement]
public string CoCNumber { get; set; }
[XmlElement]
public string PostAddressLine1 { get; set; }
[XmlElement]
public string PostPostalCode { get; set; }
[XmlElement]
public string PostCity { get; set; }
[XmlElement]
public string ExternalCreditLimitValidFrom { get; set; }
[XmlElement]
public string ExternalCreditLimitValidTo { get; set; }
[XmlElement]
public string ExternalCreditLimitType { get; set; }
[XmlElement]
public string CreditInsurerNumber { get; set; }
[XmlArray]
public List<Invoice> Invoices { get; set; }
}
public class Invoice
{
[XmlAttribute]
public string ImportCode { get; set; }
[XmlAttribute]
public string CurrencyCode { get; set; }
[XmlElement]
public string Number { get; set; }
[XmlElement]
public string Date { get; set; }
[XmlElement]
public string DueDate { get; set; }
[XmlElement]
public string Amount { get; set; }
[XmlElement]
public string AmountVAT { get; set; }
[XmlElement]
public string OpenAmount { get; set; }
[XmlElement]
public string ManualLeading { get; set; }
[XmlElement]
public string Reference { get; set; }
[XmlElement]
public string Description { get; set; }
[XmlElement]
public string OrderNumber { get; set; }
[XmlElement]
public string IncludeInCorrespondence { get; set; }
}
我想要的输出是这样的:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDebtor>
<Debtor ImportCode="ImportCode1" LanguageCode="AR" CountryCode="AF">
<Number>AR001</Number>
<ParentDebtorNumber>AQ1</ParentDebtorNumber>
<Name>Soapystuff n.v.</Name>
<AdditionalName>Soapystuff</AdditionalName>
<AddressLine1>Zeepstraat 15</AddressLine1>
<AddressLine2>bus2</AddressLine2>
<AddressLine3></AddressLine3>
<PostalCode>1000 AA</PostalCode>
<City>Amsterdam</City>
<Telephone>01-4789-457-11</Telephone>
<Mobile>06-456-789-78</Mobile>
<Fax>12364111</Fax>
<Email>info@soapystuff.nl</Email>
<CustomEmail>sales@soapystuff.nl</CustomEmail>
<AccountManagerEmail>accountmanager@tkb.nl</AccountManagerEmail>
<InternalEmail>info@tkb.nl</InternalEmail>
<Contact>mr A. Van dingenen</Contact>
<InternalCreditLimit>100000</InternalCreditLimit>
<ExternalCreditLimit>2000</ExternalCreditLimit>
<Category>B</Category>
<Accountmanager>mevr savon</Accountmanager>
<VATNumber></VATNumber>
<PaymentCondition>30 dagen</PaymentCondition>
<CoCNumber>12.345.678</CoCNumber>
<PostAddressLine1>Maanstraat 12</PostAddressLine1>
<PostPostalCode>1000 AA</PostPostalCode>
<PostCity>Amsterdam</PostCity>
<ExternalCreditLimitValidFrom>2013-01-01</ExternalCreditLimitValidFrom>
<ExternalCreditLimitValidTo>2014-12-31</ExternalCreditLimitValidTo>
<ExternalCreditLimitType></ExternalCreditLimitType>
<CreditInsurerNumber>12345</CreditInsurerNumber>
<Invoices>
<Invoice ImportCode="ImportCode1" CurrencyCode="AUD">
<Number>FN0000111</Number>
<Date>2011-08-01</Date>
<DueDate>2011-09-01</DueDate>
<Amount>1200.45</Amount>
<AmountVAT>120</AmountVAT>
<OpenAmount>1000</OpenAmount>
<ManualLeading>true</ManualLeading>
<Reference>123456</Reference>
<Description></Description>
<OrderNumber>5467</OrderNumber>
<CustomFields>
<CustomField Code="invoiceReference">XS001Ref2</CustomField>
<CustomField Code="StartDate">
2011-01-01T00:00:00</CustomField>
</CustomFields>
</Invoice>
<Invoice ImportCode="ImportCode2" CurrencyCode="AUD">
<Number>FN0000112</Number>
<Date>2011-08-11</Date>
<DueDate>2011-09-10</DueDate>
<Amount>1200.45</Amount>
<AmountVAT>120</AmountVAT>
<OpenAmount>1000</OpenAmount>
<CustomFields>
TKB IT Solutions – XML-specificaties TRUST IT
<CustomField Code="invoiceOtherCodeReference">XSRef2
</CustomField>
<CustomField Code="StartDate">
2011-01-01T12:30:00</CustomField>
</CustomFields>
</Invoice>
<Invoice ImportCode="ImportCode3" CurrencyCode="AUD">
<Number>FN0000113</Number>
<Date>2011-09-11</Date>
<DueDate>2011-11-10</DueDate>
<Amount>100.45</Amount>
<OpenAmount>75</OpenAmount>
</Invoice>
</Invoices>
<CustomFields>
<CustomField Code="debiteurReferentie">DebRef0001</CustomField>
<CustomField Code="DebiteurFinalDate">
2011-12-31T00:00:00</CustomField>
</CustomFields>
</Debtor>
<Debtor ImportCode="ImportCode2" LanguageCode="NL" CountryCode="NL">
<Number>DB0234</Number>
<Name>autos Verstraeten bvba</Name>
<Category>P</Category>
<Invoices>
<Invoice ImportCode="ImportCode1" CurrencyCode="AUD">
<Number>FN0000111</Number>
<Date>2011-08-01</Date>
<DueDate>2011-09-01</DueDate>
<Amount>1200.45</Amount>
<AmountVAT>120</AmountVAT>
<OpenAmount>1000</OpenAmount>
<ManualLeading>true</ManualLeading>
<CustomFields>
<CustomField Code="invoiceReference">XS001Ref2</CustomField>
<CustomField Code="StartDate">
2011-01-01T00:00:00
</CustomField>
</CustomFields>
</Invoice>
</Invoices>
</Debtor>
<Debtor ImportCode="ImportCode3" LanguageCode="NL" CountryCode="NL">
<Number>Q475AAAA</Number>
<Name>security Secure-it</Name>
<Category>B</Category>
</Debtor>
</ArrayOfDebtor>
我要序列化的代码:
TrustIt.TrustIt xmlObj = new TrustIt.TrustIt();
xmlObj.ArrayOfDebtor = new List<TrustIt.Debtor>();
//Debtor 1
TrustIt.Debtor Debtor = randomize<TrustIt.Debtor>(new TrustIt.Debtor());
Debtor.Name = "Debtor Name 1";
TrustIt.Invoice invoice1 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice1.ImportCode = "INV001";
invoice1.CurrencyCode = "EUR";
TrustIt.Invoice invoice2 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice2.ImportCode = "INV002";
invoice2.CurrencyCode = "EUR";
TrustIt.Invoice invoice3 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice3.ImportCode = "INV003";
invoice3.CurrencyCode = "EUR";
TrustIt.Invoice invoice4 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice4.ImportCode = "INV004";
invoice4.CurrencyCode = "EUR";
Debtor.Invoices = new List<TrustIt.Invoice>();
Debtor.Invoices.Add(invoice1);
Debtor.Invoices.Add(invoice2);
Debtor.Invoices.Add(invoice3);
Debtor.Invoices.Add(invoice4);
xmlObj.ArrayOfDebtor.Add(Debtor);
//Debtor 2
Debtor = randomize<TrustIt.Debtor>(new TrustIt.Debtor());
Debtor.Name = "Debtor Name 1";
invoice1 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice1.ImportCode = "INV010";
invoice1.CurrencyCode = "EUR";
invoice2 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice2.ImportCode = "INV011";
invoice2.CurrencyCode = "EUR";
invoice3 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice3.ImportCode = "INV012";
invoice3.CurrencyCode = "EUR";
invoice4 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice4.ImportCode = "INV013";
invoice4.CurrencyCode = "EUR";
Debtor.Invoices = new List<TrustIt.Invoice>();
Debtor.Invoices.Add(invoice1);
Debtor.Invoices.Add(invoice2);
Debtor.Invoices.Add(invoice3);
Debtor.Invoices.Add(invoice4);
xmlObj.ArrayOfDebtor.Add(Debtor);
XmlSerializer xsSubmit = new XmlSerializer(typeof(TrustIt.TrustIt));
string xml = "";
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw))
{
xsSubmit.Serialize(writer, xmlObj);
xml = sw.ToString(); // Your XML
}
}
所以要么我在 xml 的开头和结尾都有 TrustIt,但现在不应该在那里,或者现在在添加 XmlRoot 属性时我的 xml 输出中有两倍的 ArrayOfDebtor .我该如何防止这种情况?
[XmlRoot("ArrayOfDebtor")]
public class TrustIt
{
[XmlElement("Debtor")] // <==== this is the change
public List<Debtor> ArrayOfDebtor { get; set; }
}
我已经编写了序列化 class 的代码。一切正常,但我不想让我的 top class 包含在序列化中,最好的方法是什么?
示例 classes:
[XmlRoot(elementName: "ArrayOfDebtor")]
public class TrustIt
{
[XmlArray]
public List<Debtor> ArrayOfDebtor { get; set; }
}
public class Debtor
{
[XmlAttribute]
public string ImportCode { get; set; }
[XmlAttribute]
public string LanguageCode { get; set; }
[XmlAttribute]
public string CountryCode { get; set; }
[XmlElement]
public string Number { get; set; }
[XmlElement]
public string ParentDebtorNumber { get; set; }
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string AdditionalName { get; set; }
[XmlElement]
public string AddressLine1 { get; set; }
[XmlElement]
public string AddressLine2 { get; set; }
[XmlElement]
public string AddressLine3 { get; set; }
[XmlElement]
public string PostalCode { get; set; }
[XmlElement]
public string City { get; set; }
[XmlElement]
public string Telephone { get; set; }
[XmlElement]
public string Mobile { get; set; }
[XmlElement]
public string Fax { get; set; }
[XmlElement]
public string Email { get; set; }
[XmlElement]
public string CustomEmail { get; set; }
[XmlElement]
public string AccountManagerEmail { get; set; }
[XmlElement]
public string InternalEmail { get; set; }
[XmlElement]
public string Contact { get; set; }
[XmlElement]
public string InternalCreditLimit { get; set; }
[XmlElement]
public string ExternalCreditLimit { get; set; }
[XmlElement]
public string Category { get; set; }
[XmlElement]
public string Accountmanager { get; set; }
[XmlElement]
public string VATNumber { get; set; }
[XmlElement]
public string PaymentCondition { get; set; }
[XmlElement]
public string CoCNumber { get; set; }
[XmlElement]
public string PostAddressLine1 { get; set; }
[XmlElement]
public string PostPostalCode { get; set; }
[XmlElement]
public string PostCity { get; set; }
[XmlElement]
public string ExternalCreditLimitValidFrom { get; set; }
[XmlElement]
public string ExternalCreditLimitValidTo { get; set; }
[XmlElement]
public string ExternalCreditLimitType { get; set; }
[XmlElement]
public string CreditInsurerNumber { get; set; }
[XmlArray]
public List<Invoice> Invoices { get; set; }
}
public class Invoice
{
[XmlAttribute]
public string ImportCode { get; set; }
[XmlAttribute]
public string CurrencyCode { get; set; }
[XmlElement]
public string Number { get; set; }
[XmlElement]
public string Date { get; set; }
[XmlElement]
public string DueDate { get; set; }
[XmlElement]
public string Amount { get; set; }
[XmlElement]
public string AmountVAT { get; set; }
[XmlElement]
public string OpenAmount { get; set; }
[XmlElement]
public string ManualLeading { get; set; }
[XmlElement]
public string Reference { get; set; }
[XmlElement]
public string Description { get; set; }
[XmlElement]
public string OrderNumber { get; set; }
[XmlElement]
public string IncludeInCorrespondence { get; set; }
}
我想要的输出是这样的:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDebtor>
<Debtor ImportCode="ImportCode1" LanguageCode="AR" CountryCode="AF">
<Number>AR001</Number>
<ParentDebtorNumber>AQ1</ParentDebtorNumber>
<Name>Soapystuff n.v.</Name>
<AdditionalName>Soapystuff</AdditionalName>
<AddressLine1>Zeepstraat 15</AddressLine1>
<AddressLine2>bus2</AddressLine2>
<AddressLine3></AddressLine3>
<PostalCode>1000 AA</PostalCode>
<City>Amsterdam</City>
<Telephone>01-4789-457-11</Telephone>
<Mobile>06-456-789-78</Mobile>
<Fax>12364111</Fax>
<Email>info@soapystuff.nl</Email>
<CustomEmail>sales@soapystuff.nl</CustomEmail>
<AccountManagerEmail>accountmanager@tkb.nl</AccountManagerEmail>
<InternalEmail>info@tkb.nl</InternalEmail>
<Contact>mr A. Van dingenen</Contact>
<InternalCreditLimit>100000</InternalCreditLimit>
<ExternalCreditLimit>2000</ExternalCreditLimit>
<Category>B</Category>
<Accountmanager>mevr savon</Accountmanager>
<VATNumber></VATNumber>
<PaymentCondition>30 dagen</PaymentCondition>
<CoCNumber>12.345.678</CoCNumber>
<PostAddressLine1>Maanstraat 12</PostAddressLine1>
<PostPostalCode>1000 AA</PostPostalCode>
<PostCity>Amsterdam</PostCity>
<ExternalCreditLimitValidFrom>2013-01-01</ExternalCreditLimitValidFrom>
<ExternalCreditLimitValidTo>2014-12-31</ExternalCreditLimitValidTo>
<ExternalCreditLimitType></ExternalCreditLimitType>
<CreditInsurerNumber>12345</CreditInsurerNumber>
<Invoices>
<Invoice ImportCode="ImportCode1" CurrencyCode="AUD">
<Number>FN0000111</Number>
<Date>2011-08-01</Date>
<DueDate>2011-09-01</DueDate>
<Amount>1200.45</Amount>
<AmountVAT>120</AmountVAT>
<OpenAmount>1000</OpenAmount>
<ManualLeading>true</ManualLeading>
<Reference>123456</Reference>
<Description></Description>
<OrderNumber>5467</OrderNumber>
<CustomFields>
<CustomField Code="invoiceReference">XS001Ref2</CustomField>
<CustomField Code="StartDate">
2011-01-01T00:00:00</CustomField>
</CustomFields>
</Invoice>
<Invoice ImportCode="ImportCode2" CurrencyCode="AUD">
<Number>FN0000112</Number>
<Date>2011-08-11</Date>
<DueDate>2011-09-10</DueDate>
<Amount>1200.45</Amount>
<AmountVAT>120</AmountVAT>
<OpenAmount>1000</OpenAmount>
<CustomFields>
TKB IT Solutions – XML-specificaties TRUST IT
<CustomField Code="invoiceOtherCodeReference">XSRef2
</CustomField>
<CustomField Code="StartDate">
2011-01-01T12:30:00</CustomField>
</CustomFields>
</Invoice>
<Invoice ImportCode="ImportCode3" CurrencyCode="AUD">
<Number>FN0000113</Number>
<Date>2011-09-11</Date>
<DueDate>2011-11-10</DueDate>
<Amount>100.45</Amount>
<OpenAmount>75</OpenAmount>
</Invoice>
</Invoices>
<CustomFields>
<CustomField Code="debiteurReferentie">DebRef0001</CustomField>
<CustomField Code="DebiteurFinalDate">
2011-12-31T00:00:00</CustomField>
</CustomFields>
</Debtor>
<Debtor ImportCode="ImportCode2" LanguageCode="NL" CountryCode="NL">
<Number>DB0234</Number>
<Name>autos Verstraeten bvba</Name>
<Category>P</Category>
<Invoices>
<Invoice ImportCode="ImportCode1" CurrencyCode="AUD">
<Number>FN0000111</Number>
<Date>2011-08-01</Date>
<DueDate>2011-09-01</DueDate>
<Amount>1200.45</Amount>
<AmountVAT>120</AmountVAT>
<OpenAmount>1000</OpenAmount>
<ManualLeading>true</ManualLeading>
<CustomFields>
<CustomField Code="invoiceReference">XS001Ref2</CustomField>
<CustomField Code="StartDate">
2011-01-01T00:00:00
</CustomField>
</CustomFields>
</Invoice>
</Invoices>
</Debtor>
<Debtor ImportCode="ImportCode3" LanguageCode="NL" CountryCode="NL">
<Number>Q475AAAA</Number>
<Name>security Secure-it</Name>
<Category>B</Category>
</Debtor>
</ArrayOfDebtor>
我要序列化的代码:
TrustIt.TrustIt xmlObj = new TrustIt.TrustIt();
xmlObj.ArrayOfDebtor = new List<TrustIt.Debtor>();
//Debtor 1
TrustIt.Debtor Debtor = randomize<TrustIt.Debtor>(new TrustIt.Debtor());
Debtor.Name = "Debtor Name 1";
TrustIt.Invoice invoice1 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice1.ImportCode = "INV001";
invoice1.CurrencyCode = "EUR";
TrustIt.Invoice invoice2 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice2.ImportCode = "INV002";
invoice2.CurrencyCode = "EUR";
TrustIt.Invoice invoice3 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice3.ImportCode = "INV003";
invoice3.CurrencyCode = "EUR";
TrustIt.Invoice invoice4 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice4.ImportCode = "INV004";
invoice4.CurrencyCode = "EUR";
Debtor.Invoices = new List<TrustIt.Invoice>();
Debtor.Invoices.Add(invoice1);
Debtor.Invoices.Add(invoice2);
Debtor.Invoices.Add(invoice3);
Debtor.Invoices.Add(invoice4);
xmlObj.ArrayOfDebtor.Add(Debtor);
//Debtor 2
Debtor = randomize<TrustIt.Debtor>(new TrustIt.Debtor());
Debtor.Name = "Debtor Name 1";
invoice1 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice1.ImportCode = "INV010";
invoice1.CurrencyCode = "EUR";
invoice2 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice2.ImportCode = "INV011";
invoice2.CurrencyCode = "EUR";
invoice3 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice3.ImportCode = "INV012";
invoice3.CurrencyCode = "EUR";
invoice4 = randomize<TrustIt.Invoice>(new TrustIt.Invoice());
invoice4.ImportCode = "INV013";
invoice4.CurrencyCode = "EUR";
Debtor.Invoices = new List<TrustIt.Invoice>();
Debtor.Invoices.Add(invoice1);
Debtor.Invoices.Add(invoice2);
Debtor.Invoices.Add(invoice3);
Debtor.Invoices.Add(invoice4);
xmlObj.ArrayOfDebtor.Add(Debtor);
XmlSerializer xsSubmit = new XmlSerializer(typeof(TrustIt.TrustIt));
string xml = "";
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw))
{
xsSubmit.Serialize(writer, xmlObj);
xml = sw.ToString(); // Your XML
}
}
所以要么我在 xml 的开头和结尾都有 TrustIt,但现在不应该在那里,或者现在在添加 XmlRoot 属性时我的 xml 输出中有两倍的 ArrayOfDebtor .我该如何防止这种情况?
[XmlRoot("ArrayOfDebtor")]
public class TrustIt
{
[XmlElement("Debtor")] // <==== this is the change
public List<Debtor> ArrayOfDebtor { get; set; }
}