Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Serialize/Deserialize to xml with no attributes?

12 views
Skip to first unread message

cecilia ekeberg

unread,
May 10, 2012, 5:05:41 AM5/10/12
to
I'm very new to Serialize/Deserialize and I got a problem I can't seem to solve myself. I need some answers! :D

I got this Class:

[Serializable]
public class PhoneBook
{
public string FirstName
{ get; set; }
public string LastName
{ get; set; }
public int Phone
{ get; set; }
}

And I don't want to add any attributes to it. I just want to get a clean .XML file like this for ex:

<?xml version="1.0" encoding="UTF-8"?>
-<PhoneBook xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstName></FirstName>
<LastName></LastName>
<Phone></Phone>
</PhoneBook>

Is this possible? I want to use this for Serialize/Deserialize.
Thankful for all the answers I can get!

bradbury9

unread,
May 10, 2012, 5:38:43 AM5/10/12
to

cecilia ekeberg

unread,
May 10, 2012, 6:37:25 AM5/10/12
to
Yes kind of. But do you need to add dummy values? I would like to have a xml file without values. ><

bradbury9

unread,
May 11, 2012, 1:41:07 AM5/11/12
to
just call the constructor and then use the XmlSerializer without adding values to the properties.

Arne Vajhøj

unread,
May 14, 2012, 10:44:03 PM5/14/12
to
I think this example outline the options:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

namespace E
{
public class PhoneBook1
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
}
public class PhoneBook2
{
[XmlElement(IsNullable=true)]
public string FirstName { get; set; }
[XmlElement(IsNullable=true)]
public string LastName { get; set; }
public int? Phone { get; set; }
}
public class PhoneBook3 : IXmlSerializable

{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader rdr)
{
throw new NotImplementedException("Arne forgot to add
deserialization!");
}
public void WriteXml(XmlWriter wrt)
{
wrt.WriteStartElement("FirstName");
wrt.WriteString(FirstName != null ? FirstName : "");
wrt.WriteEndElement();
wrt.WriteStartElement("LastName");
wrt.WriteString(LastName != null ? LastName : "");
wrt.WriteEndElement();
wrt.WriteStartElement("Phone");
wrt.WriteString(Phone != 0 ? Phone.ToString() : "");
wrt.WriteEndElement();
}
}
public class Program
{
public static void Main(string[] args)
{
PhoneBook1 pb1 = new PhoneBook1();
XmlSerializer ser1 = new XmlSerializer(typeof(PhoneBook1));
ser1.Serialize(Console.Out, pb1);
PhoneBook2 pb2 = new PhoneBook2();
XmlSerializer ser2 = new XmlSerializer(typeof(PhoneBook2));
ser2.Serialize(Console.Out, pb2);
PhoneBook3 pb3 = new PhoneBook3();
XmlSerializer ser3 = new XmlSerializer(typeof(PhoneBook3));
ser3.Serialize(Console.Out, pb3);
Console.ReadKey();
}
}
}

Arne


0 new messages