2017-02-27 50 views
-1

我有一些序列化的问题。比方说,我有课是这样的:C#序列化 - 其他类的列表

Class Hardware 
{ 
public string cpu_name; 
public int ram_size; 
List<HardDisk> hd; 
Class HardDisk{ public string model; public int size;} 
} 

什么是得到的结果作为simpliest方式:

<HardwareInfo> 
<cpu_name> ABC Pentium xyz</cpu_name> 
<ram_size> 123 </ram_size> 
<hard_disk> 
<model>Toshiba XYZ</model> 
<size> 123 GB </size> 
</hard_disk> 
<hard_disk> 
<model>Logitech XYZ</model> 
<size> 99 GB </size> 
</hard_disk> 
</HardwareInfo> 

我已经产生了我自己的梅托德只是为了添加标签,它看起来像我写,但我也必须写我的“反序列化”的方法,如果我使用这个...

所以你能告诉我如何简单地在XML序列化?

p.s. 我已经阅读了一些类似于我的问题,但我不明白太多。我的英语不是太好:(

+2

那么,你的HardDisk类也需要可序列化。你有什么尝试? –

+0

看起来不像有效的c#。我相信'Class'应该是'class'。另外,通过我见过的任何库,序列化只适用于属性,而不适用于字段。 –

+0

@SharkyShark,答案是否满足您的需求?如果是这样,请标记出来。如果没有,请告诉我们你的困难。 –

回答

2

你的代码,如果你使用的是C#的一些问题。我已经取得了一些corretions,其中包括一些汇编信息,以获得您所期望的结果。见下面的代码。

这里你的类应该如何:

[System.Serializable] 
[System.Xml.Serialization.XmlRoot(ElementName = "hard_disk")] 
public class HardDisk 
{ 
    [System.Xml.Serialization.XmlElement] 
    public string model { get; set; } 

    [System.Xml.Serialization.XmlElement] 
    public int size { get; set; } 

    public HardDisk() { } 
} 


[System.Serializable] 
[System.Xml.Serialization.XmlRoot(ElementName = "HardwareInfo")] 
public class Hardware 
{ 
    [System.Xml.Serialization.XmlElement] 
    public string cpu_name { get; set; } 

    [System.Xml.Serialization.XmlElement] 
    public int ram_size { get; set; } 

    [System.Xml.Serialization.XmlElement(ElementName = "hard_disk")] 
    public List<HardDisk> hd { get; set; } 

    public Hardware() 
    { 
     hd = new List<HardDisk>(); 
    } 
} 

的代码使用您的预计结果作为例子连载:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var hw = new Hardware() 
     { 
      cpu_name = "ABC Pentium xyz", 
      ram_size = 123, 
      hd = new List<HardDisk>() 
      { 
       new HardDisk() { 
        model = "Toshiba XYZ", 
        size = 123 
       }, 
       new HardDisk() { 
        model = "Logitech XYZ", 
        size = 99 
       } 
      } 
     }; 

     var xml = new System.Xml.Serialization.XmlSerializer(typeof(Hardware)); 

     var ns = new System.Xml.Serialization.XmlSerializerNamespaces(); 
     ns.Add("", ""); 

     xml.Serialize(Console.Out, hw, ns); 
    } 
} 

您的结果将是:

<?xml version="1.0" encoding="UTF-8"?> 
<HardwareInfo> 
    <cpu_name>ABC Pentium xyz</cpu_name> 
    <ram_size>123</ram_size> 
    <hard_disk> 
    <model>Toshiba XYZ</model> 
    <size>123</size> 
    </hard_disk> 
    <hard_disk> 
    <model>Logitech XYZ</model> 
    <size>99</size> 
    </hard_disk> 
</HardwareInfo> 
2

你应该使用这个样本代码

Importent注

  • 序列化class马克为[Serializable] attirbute
  • 如果类名称不一样xml根标签,类将标记为[XmlRoot(ElementName = "XmlTagName")]属性
  • 如果序列化类有属性类型为另一个类,这个属性标记为[XmlElement("XmlTagName")]属性

可以检查articleSerialization & Deserialization

型号

  [Serializable] 
      [XmlRoot(ElementName = "HardwareInfo")] 
      public class Hardware 
      { 
       [XmlElement] 
       public string cpu_name { get; set; } 
       [XmlElement] 
       public int ram_size { get; set; } 

       [XmlElement("hard_disk")] 
       public List<HardDisk> hd { get; set; }     
      } 

      [Serializable] 
      [XmlRoot(ElementName = "hard_disk")] 
      public class HardDisk 
      { 
       [XmlElement] 
       public string model { get; set; } 
       [XmlElement] 
       public string size { get; set; } 
      } 

    namespace ConsoleApplication1 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       string xmlString = @"<HardwareInfo> 
             <cpu_name> ABC Pentium xyz</cpu_name> 
             <ram_size> 123 </ram_size> 
             <hard_disk> 
             <model>Toshiba XYZ</model> 
             <size> 123 GB </size> 
             </hard_disk> 
             <hard_disk> 
             <model>Logitech XYZ</model> 
             <size> 99 GB </size> 
             </hard_disk> 
            </HardwareInfo>"; 

       var result = DeSerialization<Hardware>(xmlString); 
      } 

      static T DeSerialization<T>(string xmlStrig) where T : class 
      { 
       XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));       
       using (StringReader sReader = new StringReader(xmlStrig)) 
       { 
        return (T)xmlSerializer.Deserialize(sReader); 
       } 
      } 
     }   
    }