XML を取得して逆シリアル化し、単一のオブジェクト値を更新してから、XML に (再) シリアル化しています。
取得された XML には、値を持つフィールドのみが含まれます (つまり、この XML を作成するアプリケーションは明らかに null とゼロを無視します)。
ただし、私のコード (以下) は、値を設定していない場合でも、クラス内のすべてのオブジェクトをシリアル化します。
本質的には、取得した値のみを返し、残りは無視したいと考えています。
コードは、私がやりたいことではなく、私が求めていることを正確に実行します(以下は簡潔にするために編集されていますが、関連する要素が含まれています):
Private Sub Update_Name()
'write 'name' to xml file
Dim table As New table() ' 'table' is the name of my CLASS
Dim serializer As New XmlSerializer(table.GetType())
Dim ns As New XmlSerializerNamespaces()
ns.Add("", "")
Using reader = XmlReader.Create("C:/mwName_in.xml")
table = CType(serializer.Deserialize(reader), table)
End Using
'update fields in xml table and save to file
Dim name = table.name
For Each nm In name
nm.custom4 = "Subscriber" ' this is the only value that I am setting/updating
Dim writer As XmlWriter
Using writer = XmlWriter.Create("C:/mwName_out.xml")
serializer.Serialize(writer, table, ns)
End Using
'THIS IS WHERE I POST THE RESULTING XML
Next
End Sub
私の「着信」XMLは次のようになります。
<?xml version="1.0"?>
<table found="6" start="0" count="6" name="Name">
<name>
<code>AWEBSTER</code>
<name>Alex Webster</name>
<address1>West End Road</address1>
<address2>Herne Bay</address2>
<address3>Auckland</address3>
<delivery1>West End Road</delivery1>
<delivery2>Herne Bay</delivery2>
<delivery3>Auckland</delivery3>
<delivery4>Auckland</delivery4>
<phone>021555 8888</phone>
<category1>SHOP</category1>
<category2>NZ</category2>
<customertype>2</customertype>
<debtorterms>-20</debtorterms>
<creditorterms>-20</creditorterms>
<recaccount>5500</recaccount>
<payaccount>6500</payaccount>
<suppliertype>2</suppliertype>
<email>test@test.com</email>
<productpricing>B</productpricing>
</name>
</table>
私の送信 XML は次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<table>
<name>
<hold>false</hold>
<dateoflastsale>0001-01-01T00:00:00</dateoflastsale>
<supplierpropmtpaymentterms>0</supplierpropmtpaymentterms>
<custpromptpaymentterms>0</custpromptpaymentterms>
<customertype>2</customertype>
<suppliertype>2</suppliertype>
<colour>0</colour>
<d30plus>0</d30plus>
<d60plus>0</d60plus>
<d90plus>0</d90plus>
<discount>0</discount>
<ccurrent>0</ccurrent>
<dcurrent>0</dcurrent>
<creditorterms>-20</creditorterms>
<debtorterms>-20</debtorterms>
<paymentmethod>0</paymentmethod>
<lastpaymentmethod>0</lastpaymentmethod>
<splitpercent>0</splitpercent>
<supppromptpaymentdiscount>0</supppromptpaymentdiscount>
<receiptmethod>0</receiptmethod>
<custpropmtpaymentdiscount>0</custpropmtpaymentdiscount>
<dbalance>0</dbalance>
<creditlimit>0</creditlimit>
<kind>0</kind>
<usernum>0</usernum>
<lastmodifiedtime>0001-01-01T00:00:00</lastmodifiedtime>
<abuid>0001-01-01T00:00:00</abuid>
<delivery1>West End Road</delivery1>
<delivery4>Auckland</delivery4>
<delivery2>Herne Bay</delivery2>
<delivery3>Auckland</delivery3>
<email>test@test.com</email>
<custom4>Subscriber</custom4>
<address1>West End Road</address1>
<address2>Herne Bay</address2>
<address3>Auckland</address3>
<name>Alex Webster</name>
<phone>0215558888</phone>
<productpricing>B</productpricing>
<payaccount>6500</payaccount>
<recaccount>5500</recaccount>
<code>AWEBSTER</code>
<category1>SHOP</category1>
<category2>NZ</category2>
</name>
</table>
明らかに、シリアライゼーションはクラス 'table' と 'name' のすべてのオブジェクトを取得しましたが、値が設定されたもの (つまり、着信 XML にあったものと更新した 1 つの値 - 'custom4') だけが必要です。 '、 この場合)。
問題は、どのフィールドが設定され、どのフィールドがnull/zeroになるかを事前に知らないため、XmlIgnoreを使用できないことです(少なくとも私は使用できないと思います)-そして、1つだけを更新する必要があります分野。
クラス全体は含めていませんが、発信 XML (上記) に正常にシリアル化されます。
よろしくお願いします。これはちょっと私を夢中にさせています。