2014-12-04 67 views
1

我有一個對象:Xmlserializer nillable元素與xmlattribute,可能嗎?

class Thing { 
    [Xmlarray("Widget", IsNullable=true) ] 
    List<Widget> Widgets; 
} 

class Widget { 
    [Xmlattribute] 
    public string Name; 
    [XmlTextAttribute] 
    public string Value; 
} 

基本上我想樣本輸出的樣子:

<Thing> 
    <Widget name="foo" xsi:nil="true"/> 
    <Widget name="bar">Nerds</Widget> 
</Thing> 

我遇到的問題是,XmlSerializer的不這樣做,爲Foo線。它不會爲包含null的小部件寫出xsi:nil位。這只是一個空的元素(<Widget name="foo"/>

該結束了吃這個XML是老了,垃圾和在我的掌握,希望其零位在那裏,如果我想要一個小部件記錄從系統中刪除解析器/存儲與將其設置爲空(這是它在空的Widget條目中缺少零位時的作用)

對不起,如果出現錯誤,將它寫在手機上基本上我該如何讓xmlserializer寫入零位?

UPDATE:這裏是實際的標籤,我正在閱讀有關如何不能設置nillable如果有一個屬性在arrayitem上(Widgets列表中的Widget)。

<Widget xsi:nil="true"/> 

對我沒用,因爲我已經提到的 - 項需要name屬性和零=真(它告訴處理器「這一領域,從商店中刪除」)。沒有名稱屬性,它不知道什麼字段。可悲的是,它只依賴於xsi:nil來告訴它。如果它看到一個空的<Widget name="foo"/> - 它將其設置爲空/空,而不是完全刪除。

class Thing{ 
     [System.Xml.Serialization.XmlArrayItemAttribute("Widget", IsNullable=true)] 
     public List<Widget> Widgets { get; set; } 
} 
class Widget{ 
     [System.Xml.Serialization.XmlAttribute][JsonProperty] 
     public string name {get;set;} 
     [System.Xml.Serialization.XmlTextAttribute] 
     public string Value {get;set;} 
} 

本質上講,它不可能是<Widget name="foo"><Value>Bar</Value></Widget><Widget xsi:nil=true/><Widget name="foo"/> - 必須是唯一的<Widget name="foo" xsi:nil="true"/>。責備這件事發送到處理器(我無法控制)。

那麼,它是可序列化的嗎?

回答

2

我更新了答案,並刪除了不相關的代碼。實現IXmlSerializable的可以爲此issue.I工作只實現接口中WriteXML功能,你可以,如果你need.The代碼將被改變爲初級講座實施其他:

public class Thing:IXmlSerializable 
    {  
     public List<Widget> Widgets{get;set;} 

     public void GetObjectData(SerializationInfo info, StreamingContext context) 
     { 
      throw new NotImplementedException(); 
     } 

     public System.Xml.Schema.XmlSchema GetSchema() 
     { 
      throw new NotImplementedException(); 
     } 

     public void ReadXml(System.Xml.XmlReader reader) 
     { 
      throw new NotImplementedException(); 
     } 

     public void WriteXml(System.Xml.XmlWriter writer) 
     { 
      writer.WriteStartElement("xsi","Thing", @"http://www.w3.org/2001/XMLSchema-instance"); 
      foreach (Widget widget in Widgets) 
      { 
       if (string.IsNullOrEmpty(widget.Value)) 
       { 
        writer.WriteStartElement("widget"); 
        writer.WriteAttributeString("Name", widget.Name); 
        writer.WriteAttributeString("xsi", "nil", @"http://www.w3.org/2001/XMLSchema-instance", "true");         
        writer.WriteEndElement(); 
       } 
       else 
       { 
        writer.WriteStartElement("widget"); 
        writer.WriteAttributeString("Name", widget.Name); 
        writer.WriteString(widget.Value); 
        writer.WriteEndElement(); 
       } 
      } 
      writer.WriteEndElement(); 
      writer.Flush(); 
     } 
    } 

    public class Widget 
    { 
     public string Name{get;set;} 
     public string Value { get; set; } 
    } 
} 

public static void SaveXml() 
     { 
      XmlWriterSettings settings= new XmlWriterSettings(); 
      settings.Indent = true; 
      settings.OmitXmlDeclaration = true;     

      XmlWriter xmlWriter = XmlWriter.Create(@"c:\test.xml",settings);   
      thing.WriteXml(xmlWriter); 

     } 

系列化後,XML看起來像below.widget3的值爲null。希望這有幫助。

<xsi:Thing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <widget Name="name1">widget1</widget> 
     <widget Name="name2">widget2</widget> 
     <widget Name="name3" xsi:nil="true" /> 
    </xsi:Thing> 
+0

我可能寫錯了Xmlarray屬性。這是一個序列號提示,確實接受IsNullable。不幸的是我無法改變最終結果。它被餵給一個糟糕的老式和蹩腳的處理器,並不總是正確的。 – Jason 2014-12-04 12:42:54

+0

當我進入辦公室並關閉手機時,我會編輯帖子以更清晰並提供我的問題的鏈接。 – Jason 2014-12-04 12:47:05

+0

我已更新帖子以包含更好的信息。 – Jason 2014-12-04 15:50:29