2013-05-30 48 views
0

2Let說,我有此數組:C#XML序列化XmlAttribute

<something> 
    <items1 note="some text"> 
     <item1></item1> 
     <item1></item1> 
     <item1></item1> 
    </items1> 
    <items2> 
     <item2></item2> 
     <item2></item2> 
     <item2></item2> 
    </items2> 
</something> 

而且我有一個模型:

public class Something 
{ 
    public string Item1Note { get; set; } 
    public List<Item1> Items1 { get; set; } 
    public List<Item2> Items2 { get; set; } 
} 

因此,是否有可能反序列化XML到模型,使屬性記Items1節點在屬性Item1Note中。 Thx提前。

編輯:據我所知,該筆記屬於Items1,但我沒有這樣的類。

+0

這是'items1'不'something' – I4V

+0

注意的屬性只是一個子元素,用了'something'根,什麼是模型解串器可以識別。 – Saravanan

+0

你能告訴我這個代碼嗎? – Kindzoku

回答

2

類是將XML

public class Items1 
{ 
    [XmlAttribute] 
    public string note { get; set; } 
    [XmlElement] 
    public List<item1> item1 { get; set; } 
} 

public class Item2 
{ 
    [XmlElement] 
    public List<item2> item2 { get; set; } 
} 

[XmlRootAttribute("Something", Namespace="", IsNullable=false)] 
public class Something 
{ 
    [XmlElement] 
    public Items1 items1 { get; set; } 
    [XmlElement] 
    public Item2 item2 { get; set; } 
} 


Something objSomething = this.Something(); 

ObjectXMLSerializer<Something>.Save(objSomething, FILE_NAME); 

Loading the xml 

objSomething = ObjectXMLSerializer<Something>.Load(FILE_NAME); 
+0

重點是我不想改變模型,所以這是不可能的當前模型?Thx。 – Kindzoku

+0

如果你看到我改變了模型..我提到了兩件事一個xmlelement和xmlattribute來解釋你將如何註釋被coiming你想要生成xml或讀取xml –

+0

並使用objectxml序列化程序我加載並保存對象 –