2012-12-03 124 views
0

我怎樣才能序列化使用ProductXmlSerializer的屬性和多個元素的

  • 只有1個實例的Updates

  • UpdatesXmlSerializer

    1. 只有1實例可以包含多個Item以下結構

    2. Item可以包含多個Artifact

    XML:

    <Product> 
        <Cycle Type = "x0446" /> 
        <Brand Type = "z773g" Include="All" /> 
        <Updates> 
         <Item Name = "Foo"> 
          <Artifact Kind="6" Action="3" /> 
         </Item> 
         <Item Name = "Bar"> 
          <Artifact Kind="6" Action="3" /> 
          <Artifact Kind="53" Action="3" /> 
         </Item> 
        </Updates> 
    </Product> 
    
  • 回答

    0

    這裏是一些代碼(一個屬性錯過了)

    using System; 
    using System.Xml; 
    using System.Xml.Serialization; 
    using System.Collections.Generic; 
    
    [Serializable] 
    [XmlRoot("Product")] 
    public class Product 
    { 
        [XmlElement("Cycle")] 
        public Cycle Cycle { get; set; } 
    
        [XmlElement("Updates")] 
        public Updates Updates { get; set; } 
    
        public Product() 
        { 
         Cycle = new Cycle(); 
         Updates = new Updates(); 
        } 
    } 
    
    [Serializable] 
    public class Cycle 
    { 
        [XmlAttribute("Type")] 
        public string Type { get; set; } 
    } 
    
    [Serializable] 
    public class Updates 
    { 
        [XmlElement("Item")] 
        public List<Item> Items { get; set; } 
    
        public Updates() 
        { 
         Items = new List<Item>(); 
        } 
    } 
    
    [Serializable] 
    public class Item 
    { 
        [XmlElement("Artifact", typeof(Artifact))] 
        public List<Artifact> Artifacts { get; set; } 
    
        public Item() 
        { 
         Artifacts = new List<Artifact>(); 
        } 
    } 
    
    [Serializable] 
    public class Artifact 
    { 
        [XmlAttribute("Kind")] 
        public int Kind { get; set; } 
    } 
    

    這裏的序列化代碼

    using System; 
    using System.Xml; 
    using System.Xml.Serialization; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Text; 
    
        private static string SerializeMe(object o) 
        { 
         XmlSerializer s = new XmlSerializer(o.GetType()); 
    
         MemoryStream stream = new MemoryStream(); 
    
         XmlWriter writer = new XmlTextWriter(stream, Encoding.Default); 
    
         s.Serialize(writer, o); 
    
         stream.Flush(); 
    
         stream.Seek(0, SeekOrigin.Begin); 
    
         return Encoding.Default.GetString(stream.ToArray()); 
        } 
    

    現在這裏一個控制檯上

    static void Main(string[] args) 
        { 
         Product p = new Product(); 
    
         p.Cycle.Type = "whatever"; 
    
         Item i = new Item(); 
         i.Artifacts.Add(new Artifact{Kind = 45}); 
    
         p.Updates.Items.Add(i); 
    
         Console.WriteLine(SerializeMe(p)); 
    
         Console.ReadLine(); 
        } 
    

    希望測試代碼這有助於:)

    1

    你可以control xml serialization using attributes。使用XmlAttribute屬性,將默認序列化更改爲元素序列化爲屬性。使用XmlElement屬性將列表序列化爲xml元素的平面序列。

    public class Product 
    { 
        public Cycle Cycle { get; set; } 
        public Brand Brand { get; set; } 
        public List<Item> Updates { get; set; } 
    } 
    
    public class Cycle 
    { 
        [XmlAttribute("Type")] 
        public string Type { get; set; }  
    } 
    
    public class Brand 
    { 
        [XmlAttribute("Type")] 
        public string Type { get; set; } 
        [XmlAttribute("Include")] 
        public string Include { get; set; } 
    } 
    
    public class Item 
    { 
        [XmlAttribute("Name")] 
        public string Name { get; set; } 
        [XmlElement("Artifact")] 
        public List<Artifact> Artifacts { get; set; } 
    } 
    
    public class Artifact 
    { 
        [XmlAttribute("Kind")] 
        public int Kind { get; set; } 
        [XmlAttribute("Action")] 
        public int Action { get; set; } 
    } 
    

    連載:

    Product p = new Product() 
    { 
        Cycle = new Cycle() { Type = "x0446" }, 
        Brand = new Brand() { Type = "z773g", Include = "All" }, 
        Updates = new List<Item>() 
        { 
         new Item() { Name = "Foo", 
            Artifacts = new List<Artifact>() { 
             new Artifact() { Action = 3, Kind = 6 } 
            } 
         }, 
         new Item() { Name = "Bar", 
            Artifacts = new List<Artifact>() { 
             new Artifact() { Action = 3, Kind = 6 }, 
             new Artifact() { Action = 3, Kind = 53 }, 
            } 
         } 
        } 
    }; 
    
    XmlSerializer serializer = new XmlSerializer(typeof(Product)); 
    Stream stream = new MemoryStream(); // use whatever you need 
    serializer.Serialize(stream, p); 
    

    結果:

    <Product> 
        <Cycle Type = "x0446" /> 
        <Brand Type = "z773g" Include="All" /> 
        <Updates> 
         <Item Name = "Foo"> 
          <Artifact Kind="6" Action="3" /> 
         </Item> 
         <Item Name = "Bar"> 
          <Artifact Kind="6" Action="3" /> 
          <Artifact Kind="53" Action="3" /> 
         </Item> 
        </Updates> 
    </Product> 
    
    3

    您可以使用[XmlElement的]的陣列或列表屬性屬性和XmlSerializer的是足夠聰明的拿起那個,並列出它們在你的xml中一個接一個。

    OR

    您可以使用Visual Studio XML與班級發生器更復雜的結構: - 點擊開始菜單在Windows - 點擊 「所有程序」 - 找到的Microsoft Visual Studio文件夾,然後點擊 - 單擊Visual Studio工具文件夾 - 單擊開發人員命令提示符... - 假設您的xml保存在C:\ test \ Sample.xml中 - 鍵入「xsd C:\ test \ Sample.xml/out :C:\ test「 這應該告訴你一個架構已經創建。 - 鍵入「xsd C:\ test \ Sample.xsd/c/out:C:\ test」 這應該會告訴您一個.cs類是爲您創建的,將其複製到您的解決方案中,可能會使用名稱空間更改對於它的xsd命令參數) - 創建的類可能會被命名爲奇怪,並且更難於使用,如果您有複雜的XML或模式,請使用此方法。 - 你可以用擴展部分類(看看它)生成的代碼,不修改生成的代碼直接

    -

    這裏的直接編碼(不產生)會是什麼樣子:

    public class Product{ 
        [XmlElement] 
        public Cycle Cycle {get;set;} 
    
        [XmlElement] 
        public Brand Brand {get;set;} 
    
        [XmlElement] 
        public Updates Updates {get;set;} 
    } 
    
    public class Updates{ 
        [XmlElement("Item")] 
        public UpdateItem[] Items{get;set;} 
    } 
    
    public class UpdateItem{ 
        [XmlAttribute] 
        public string Name{get;set;} // use [XmlAttribute] in Cycle, Brand and Artifact classes 
    
        [XmlElement("Artifact")] 
        public Artifact[] Artifact{get;set;} 
    } 
    //.... etc 
    

    ,這裏是生成的代碼是什麼樣子:

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
    public partial class ProductUpdatesItem { 
    
    private ProductUpdatesItemArtifact[] artifactField; 
    
    private string nameField; 
    
    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Artifact", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public ProductUpdatesItemArtifact[] Artifact { 
        get { 
         return this.artifactField; 
        } 
        set { 
         this.artifactField = value; 
        } 
    } 
    

    [/代碼]

    0

    如果您有一個用於此結構的XML模式(或可以生成一個) - 我的首選是使用XSD.exe生成該類。

    相關問題