2017-05-29 56 views
0

您好我有一個用下面的XML提要我的應用程序的Web服務其序列:反序列化XML到C#對象,並以不同的方式

<Value><TABLE> 
    <PRODUCT> 
     <ProductID> 1 </ProductID> 
     <Category> sport </Category> 
     <Description> calcio </Description> 
     <Name> palla </Name> 
     <Price> 10 </Price> 
    </PRODUCT> 
    <PRODUCT> 
     <ProductID> 2 </ProductID> 
     <Category> sport </Category> 
     <Description> tennis </Description> 
     <Name> racchetta </Name> 
     <Price> 100 </Price> 
    </PRODUCT> 
    <PRODUCT> 
     <ProductID> 3 </ProductID> 
     <Category> sport </Category> 
     <Description> golf </Description> 
     <Name> borsa </Name> 
     <Price> 150 </Price> 
    </PRODUCT> 
</TABLE></Value> 

我寫了下面的對象模型和我設法正確地將反序列化使用XmlSerializer。現在

[XmlRoot("Value")] 
public class Value { 
    [XmlElement("TABLE")] 
    public TABLE TABLE { get; set; } 
} 

public class TABLE { 
    [XmlElement("PRODUCT")] 
    public List<Product> Products { get; set; } 
} 


public class Product { 
    public int ProductID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Category { get; set; } 
    public int Price { get; set; } 
} 

,我最終添加新的產品對象,我想序列化模型到和XML,以將其發送回Web服務。問題是,接受XML結構略低不同,我想的XML結構是這樣的:

<setProdotti> 
<streams> 
<instream> 
<Value><TABLE> 
     <PRODUCT> 
      <ProductID> 1 </ProductID> 
      <Category> sport </Category> 
      <Description> calcio </Description> 
      <Name> palla </Name> 
      <Price> 10 </Price> 
     </PRODUCT> 
</TABLE></Value> 
</instream> 
</streams> 
</setProdotti> 

基本上相同,與嵌入標籤setProdotti,溪流和河道內的異常的輸入XML ,這是固定的和已知的(可以硬編碼)。 它可以用當前模型完成嗎?我嘗試使用XmlSerializer的Serialize方法,但id輸出基於模型的XML(當然)以及我想避免的根元素上的標記。

<?xml version="1.0" encoding="utf-16"?><Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<TABLE> 
<PRODUCT><ProductID>1</ProductID><Name> palla </Name><Description> nike </Description><Category> calcio </Category><Price>10</Price></PRODUCT> 
</TABLE> 
</Value> 

感謝您的幫助。

+0

最簡單的解決方案是使用必要的元素名稱返回嵌套在三個容器類中的'Value'對象。有沒有理由不能這樣做? – dbc

+1

您可以使用'XmlWriter'手動在xml周圍創建三個元素。 –

回答

0

繼亞歷山大·彼得羅夫的建議,我看着詳細的XmlWriter 產生的代碼滿足我的要求是:

  XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
      ns.Add("", ""); 
      XmlSerializer xmlserializer = new XmlSerializer(typeof(Value)); 
      StringWriter stringWriter = new StringWriter(); 
      XmlWriter writer2 = XmlWriter.Create(stringWriter, new XmlWriterSettings { 
       OmitXmlDeclaration = true, 
       ConformanceLevel = ConformanceLevel.Fragment 
      }); 
      writer2.WriteStartElement("setProdotti"); 
      writer2.WriteStartElement("streams"); 
      writer2.WriteStartElement("instream"); 
      xmlserializer.Serialize(writer2, p, ns); 
      writer2.Dispose(); 
      string serializedXml = stringWriter.ToString(); 

XmlSerializerNamespaces的部分避免XML的創造價值元素的屬性(P ),並且我設置了XmlWriterSetting,以便在字符串的開頭沒有XML聲明。 顯然.Dispose()關閉我用.WriteStartElement打開的標籤。我正在尋找.Close()方法,但它不再存在。