2016-12-29 97 views
0

我對XML很陌生。我想序列化一個類,使用XMLSerializer生成以下輸出:帶有列表<>到XML的序列化類

<?xml version="1.0" encoding="UTF-8"?> 
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="DBK100.xsd"> 
<header> 
    <return-code>DBK100</return-code> 
    <return-desc>Daily Net Open Position</return-desc> 
    <inst-code> </inst-code> 
    <inst-name> </inst-name> 
    <as-at-date> </as-at-date> 
</header> 
<body> 
    <return-data> 
     <item-code>10010</item-code> 
     <position-component>1. Net Assets</position-component> 
     <us-dollar> 
      <amount>23423</amount> 
      <nature-of-position>vxgfxdfd</nature-of-position> 
     </us-dollar> 
     <gbp> 
      <amount></amount> 
      <nature-of-position></nature-of-position> 
     </gbp> 
    </return-data> 
</body> 
</return> 
+0

首先,您需要創建一個具有類似結構的類。在創建類之後,使用XmlSerializer類。如果您仍然有困難,請發佈您的代碼。 –

+0

您應該首先將類本身添加到問題中。並且總是添加你自己試圖實現你的目標。 –

回答

0

1.請使用System.Xml作爲參考添加; 2.以這種方式製作一個名爲Item的類

 public class Item 
      { 
       public string itemCode { get; set; } 
       public string positionComponent { get; set; } 
       public decimal dollarAmount { get; set; } 
       public decimal gdbAmount { get; set; } 
     } 

    try 
       { 
        XmlDocument xmlDoc = new XmlDocument(); 
        xmlDoc.Load("Write down full path"); 
        XmlNodeList dataNodes = xmlDoc.SelectNodes("/return-data"); 

        foreach (XmlNode node in dataNodes) 
        { 
         Item objItem = new Item(); 

        objItem.itemCode=node.SelectSingleNode("item-code").InnerText; 
objItem.positionComponent=node.SelectSingleNode("position-component").InnerText; 
objbook.dollarAmount=Convert.ToDecimal(node.SelectSingleNode("us-dollar/amount").InnerText); 

objbook.gdbAmount=Convert.ToDecimal(node.SelectSingleNode("gdb/amount").InnerText); 

        } 

       } 
catch(Exception ex) 
{ 
throw ex; 
}