2013-07-14 68 views
0

我正在检索和反序列化XML,更新单个对象值,然后(重新)序列化为XML。序列化为XML并忽略没有设置值的对象

检索到的XML只包含具有值的字段(即创建此XML的应用程序明显忽略空值和零)。

但是,我的代码(下面)序列化类中的所有对象,即使我没有设置值。

实质上,我只想返回我检索到的值并忽略其余值。

我只想说,代码不正是我要求它做的,只是不是我想要它做的(下面编辑为简洁但包括有关内容):

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>[email protected]</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>[email protected]</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> 

EVID目前,序列化已经获得了类“表”和“名称”中的所有对象,但我只想要设置值的那些对象(即,那些在传入的XML和我更新的一个值 - 'custom4',在这种情况下)。

问题是我不能使用XmlIgnore(至少我不认为我可以),因为我事先不知道哪些字段将被设置,哪些将为null /零 - 我只需要更新一个字段。

我没有包括整个类 - 它成功地序列化到传出的XML(上面)。

非常感谢提前。这有点让我发疯。

回答

1

为了达到这个目的,水合一个整个对象看起来有点矫枉过正,然后将它重新序列化回XML。相反,你可以使用System.Xml.Linq命名空间如下(在C#中的代码,但很容易转移到VB.NET)

var doc = XDocument.Load("c:/oldfile.xml"); 
var custom4 = doc.Root.Element("name").Element("custom4"); 
custom4.Value = "whatever"; 
doc.Save("c:/newfile.xml"); 
+0

谢谢CSJ。我明白你的观点,LINQ可能是一条可行的路。这是一个更大项目的一部分,课程已经见证了,我一直认为我是在遵循最佳实践,直到我陷入僵局。 –

相关问题