2010-06-03 77 views
1

我正在尋找通過xml序列化創建xml的連擊解決方案。我需要什麼,我有這樣的使用c創建XML的問題#

<product Id="1"> 
    <name>2 1/2 X 6 PVC NIPPLE TOE SCH 80</name> 
    <notes> 
    <note>!--note 1---</note> 
    <note>!--note 2--</note> 

    ...... 
    </notes> 

</product> 

給定的格式我在做什麼在這裏,我創造了這樣

public class product 
{ 
    [XmlElement("name")] 
    public string Name { get; set; } 

    [XmlArray("notes")] 
    public List<notes> ListNotes { get; set; } 
} 

public class notes 
{ 
    [XmlIgnore] 
    public string Note { get; set; } 
    } 

一2班,當我序列化在這本然後我得到的XML甲酸鹽

<product Id="1"> 
     <name>2 1/2 X 6 PVC NIPPLE TOE SCH 80</name> 
     <notes> 
     <notes> 
     <note>!--note 1---</note> 
     <note>!--note 2--</note> 
     </notes> 
     </notes> 
    </product> 

我不想額外<notes>。任何連擊解決方案來解決這個問題? 感謝


解決方案

public class product 
    { 
     [XmlElement("name")] 
     public string Name { get; set; } 

     [XmlArray("notes")] 
     public List<notes> ListNotes { get; set; } 
    } 

    public class notes 
    { 
     [XmlText] 
     public string Note { get; set; } 
     } 




product ObjProduct = new product 
    { 
    Name ="Pankaj", 
    notes=new List<note>() 
    } 

foreach (var note in item.ListNote) 
    { 
    ObjProduct.notes.Add(new Highmark.BLL.Common.note { Note = EncodeTo64(note.Note) }); 
    } 

現在使用這個ObjProduct進行序列化。

+0

感謝紳士,我得到解決方案。 – Pankaj 2010-06-03 07:29:28

回答

2

嘗試這樣的:

[XmlRoot("product")] 
public class Product 
{ 
    [XmlAttribute] 
    public int Id { get; set; } 

    [XmlElement("name")] 
    public string Name { get; set; } 

    [XmlElement("note")] 
    public List<Note> ListNotes { get; set; } 
} 

public class Note 
{ 
    [XmlText] 
    public string Text { get; set; } 
} 

class Program 
{ 
    public static void Main() 
    { 
     var p = new Product 
     { 
      Id = 1, 
      Name = "2 1/2 X 6 PVC NIPPLE TOE SCH 80", 
      ListNotes = new List<Note> 
      { 
       new Note { Text = "!--note 1---" }, 
       new Note { Text = "!--note 2---" }, 
      } 
     }; 
     var serializer = new XmlSerializer(p.GetType()); 
     serializer.Serialize(Console.Out, p); 
    } 
} 

如果你想從根節點刪除的命名空間:

var ns = new XmlSerializerNamespaces(); 
ns.Add("", ""); 
serializer.Serialize(Console.Out, p, ns); 
+1

對不起@@達林..這是做這樣 ZmVqaHQ = - c2RmZ2E = Pankaj 2010-06-03 06:29:30

+1

@Pankaj,請參閱我的更新。 – 2010-06-03 06:30:14

+0

謝謝@@ Darin ...這個解決方案幫我解決了我的問題 – Pankaj 2010-06-03 07:35:55

0

這並不直接回答你的問題,但如果你不能解決這個問題,你可以創建一個Schema.xsd文件,並使用.NET的XSD工具爲你生成正確的序列化類。

我已經取得了相當不錯的成功。

使用模式的明顯好處是在序列化之前驗證XML。

1

嘗試這樣的:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var product = new Product() { Name = "PVC SCHOOL" }; 
     product.Notes = new List<note>(); 
     product.Notes.Add(new note() { Note = "A" }); 
     product.Notes.Add(new note() { Note = "B" }); 

     var serialer = new XmlSerializer(typeof(Product)); 
     using (var stream = new StreamWriter("test.txt")) 
     { 
      serialer.Serialize(stream, product); 
     } 
    } 
} 


public class Product 
{ 
    [XmlElement("name")] 
    public string Name { get; set; } 

    [XmlArray("notes")] 
    public List<note> Notes { get; set; } 
} 

public class note 
{ 
    [XmlIgnore] 
    public string Note { get; set; } 
} 
+0

謝謝@@ Soe ..但是這是忽略筆記數據並顯示像這樣的結果 Pankaj 2010-06-03 07:04:12