views:

68

answers:

4

I am searching a batter solution for creating xml through xml serialization. What i need, i have a given format like this

<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>

what i am doing here, i created a 2 classes like this

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; }
  }

when i am serializing this then i am getting xml in this formate

 <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>

i don't want extra <notes>. Any batter solution to solve this problem? Thanks


Solution

 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) });
   }

Now use this ObjProduct for serialization.

A: 

This doesn't directly answer your question, but if you can't figure out this problem, you can create a Schema.xsd file, and use .NET's XSD tool to generate the proper serialization classes for you.

I've had pretty good success with that.

The obvious benefit of using a schema is the validation on XML, prior to serialization.

Alan
+2  A: 

Try like this:

[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);
    }
}

And if you want to remove the namespace from the root node:

var ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(Console.Out, p, ns);
Darin Dimitrov
sorry @@Darin.. this is doing like <note><Note>ZmVqaHQ=</Note></note>−<note><Note>c2RmZ2E=</Note></note>
Pankaj
@Pankaj, please see my update.
Darin Dimitrov
Thanks @@Darin... this solution help me solve my problem
Pankaj
+1  A: 

Try like this:

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; }
}
Soe Moe
Thanks @@Soe.. but this is ignoring note data and showing result like this <notes><notes/></notes>
Pankaj
A: 

Check out OXM which is a POCO approach to xml serialization by using a mapping API which gives you an exact control over the generated XML.

Delucia