2011-01-27 148 views
0

我有這樣的結構的XML文件:C#XML序列化

<panel> 
<tr> 
    <td> 
      <element> 
       ... smth 
      </element> 
    </td> 
    <td> 
      <element> 
       ... smth 
      </element> 
    </td> 
    <td> 
      <element> 
       ... smth 
      </element> 
    </td> 
</tr> 
<tr> 
    <td> 
      <element> 
       ... smth 
      </element> 
    </td> 
    <td> 
      <element> 
       ... smth 
      </element> 
    </td> 
</tr> 
</panel> 

這是我的對象結構:

public class Panel 
{ 
    [XmlArray(ElementName="tr")] 
    [XmlArrayItem(ElementName="td")] 
    public List<tr> Tr { get; set; } 

} 
public class tr 
{ 
    [XmlArray(ElementName="tr")] 
    [XmlArrayItem(ElementName="td")] 
    public List<td> td { get; set; } 
} 

public class td 
{ 
    public Element Element { get; set; } 
} 

反序列化後我有5 TR類對象面板。你能不能幫我糾正註解

+0

我想你的意思是把關閉 「」 XML部分中的標籤? – Ocelot20 2011-01-27 11:02:14

回答

1

這將工作(在你的例子中沒有指定爲「元」級的內容,我只是做了一串它):

[XmlRoot(ElementName="panel")] 
public class Panel 
{ 
    [System.Xml.Serialization.XmlElementAttribute("tr")] 
    public List<Tr> tr { get; set; } 
} 

public class Tr 
{ 
    [System.Xml.Serialization.XmlElementAttribute("td")] 
    public List<Td> td { get; set; } 
} 

public class Td 
{ 
    [System.Xml.Serialization.XmlElementAttribute("element")] 
    public Element Element { get; set; } 
} 

public class Element 
{ 
    [System.Xml.Serialization.XmlText] 
    public string prop { get; set; } 
} 
+0

它工作完美。謝謝!我已將XmlElementAttribute更改爲XmlElement – user592040 2011-01-27 11:12:23

0

不是應該..

public class Panel 
{ 
    [XmlArray(ElementName="panel")] //instead of [XmlArray(ElementName="tr")] 
    [XmlArrayItem(ElementName="tr")] 
    public List<tr> Tr { get; set; } 
. 
. 
} 
+0

它返回空tr列表 – user592040 2011-01-27 11:10:20

0

爲您的序列整個代碼:

namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     var lst = new List<Td>(); 

     var p = new Panel(); 
     p.Tr = new List<tr>(); 
     var tr = new tr(); 
     p.Tr.Add(tr); 
     tr.td = new List<Td>(); 
     tr.td.Add(new Td() { Element = "Val1" }); 
     tr.td.Add(new Td() { Element = "Val2" }); 

     var xmlRel = SerializeObject(p); 


     FileStream fs = new FileStream("ser.xml", FileMode.Create); 
     StreamWriter sw = new StreamWriter(fs); 
     sw.Write(xmlRel); 
     sw.Close(); 
     fs.Close(); 


    } 

    public static String SerializeObject(Object pObject) 
    { 
     try 
     { 
      String XmlizedString = null; 
      MemoryStream memoryStream = new MemoryStream(); 
      XmlSerializer xs = new XmlSerializer(typeof(Panel)); 
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 

      xs.Serialize(xmlTextWriter, pObject); 
      memoryStream = (MemoryStream)xmlTextWriter.BaseStream; 
      XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); 
      return XmlizedString; 
     } 
     catch (Exception e) 
     { 
      System.Console.WriteLine(e); 
      return null; 
     } 
    } 

    private static String UTF8ByteArrayToString(Byte[] characters) 
    { 
     UTF8Encoding encoding = new UTF8Encoding(); 
     String constructedString = encoding.GetString(characters); 
     return (constructedString); 
    } 
} 

[XmlRoot(ElementName = "panel")] 
public class Panel 
{ 
    [XmlElementAttribute("tr")] 
    public List<tr> Tr { get; set; } 

} 
public class tr 
{ 
    [XmlElementAttribute("td")] 
    public List<Td> td { get; set; } 
} 


public class Td 
{ 
    [XmlElementAttribute("element")] 
    public string Element { get; set; } 
} 

}