1

I am using this method to transform an object to XML:

protected XmlDocument SerializeAnObject(object obj)
{
    XmlDocument doc = new XmlDocument();
    DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
    MemoryStream stream = new MemoryStream();
    try
    {
        serializer.WriteObject(stream, obj);
        stream.Position = 0;
        doc.Load(stream);
        return doc;
    }
    finally
    {
        stream.Close();
        stream.Dispose();
    }
}

Eventually I get something like:

<CaCT>
  <CTC i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/a.b.BusinessEntities.InnerEntities" /> 
  <CTDescr xmlns="http://schemas.datacontract.org/2004/07/a.b.BusinessEntities.InnerEntities">blabla</CTDescr> 
  <CaId>464</CaId> 
  </CaCT>

How can I get rid of the i:nil="true" and the xmlns="http://schemas.datacontract.org/2004/07/a.b.BusinessEntities.InnerEntities"?

3
  • A: what is the object? How can we help if we can't see it? B: for fine-grained XML control, XmlSerializer is generally preferable to DataContractSerializer Commented Oct 16, 2011 at 8:01
  • @Marc Gravell: But XmlSerializer don't know how to handle interfaces like IEnumerable. Commented Oct 16, 2011 at 8:07
  • if I had to compare the features of the two, the lack of IEnumerable (easily circumvented with lists etc) would pale into insignificance compared to, say, the inability to control attributes. Commented Oct 16, 2011 at 8:11

2 Answers 2

2

Personally I've always found that hand-written XML serialization with LINQ to XML works well. It's as flexible as you want, you can make it backward and forward compatible in whatever way you want, and obviously you don't end up with any extra namespaces or attributes that you don't want.

Obviously it becomes more complicated the more complicated your classes are, but I've found it works very well for simple classes. It's at least an alternative to consider.

Sign up to request clarification or add additional context in comments.

9 Comments

I need a generic method that receives an object. I cannot write a serializer for each type.
@Naor: Do you mean you don't know what types you'll expect? How many types might there be? If you want it to be truly generic and only ever written and read by code, why do you care about the namespace etc appearing in the XML?
I have more them 30 types to serialize. For some reasons I don't know, the namespace appearing in the XML prevent me to transform the data with xslt file.
@Naor: If you're using automated serialization I would steer well clear of using XSLT with it at all - although it should be entirely possible to use XSLT with namespaced XML.
@Naor - namespaces do indeed make xslt more awkward, but that is true of all xml operations (not just xslt); it should be entirely possible to use xslt - but probably not necessary unless you have needs you haven't stated
|
2
protected string SerializeAnObject(object obj)
{
    XmlSerializerNamespaces xmlNamespaces = new XmlSerializerNamespaces();
    xmlNamespaces.Add("", "");

    XmlWriterSettings writerSettings = new XmlWriterSettings();
    writerSettings.OmitXmlDeclaration = true;

    XmlSerializer serializer = new XmlSerializer(obj.GetType());
    using (MemoryStream ms = new MemoryStream())
    {
        using (XmlWriter stream = XmlWriter.Create(ms, writerSettings))
        {
            serializer.Serialize(stream, obj, xmlNamespaces);
            return Encoding.UTF8.GetString(ms.ToArray());
        }
    }
}

1 Comment

Minor note - you could serialize to a StringEriter or similar with less indirection - but this is essentially how I would approach it, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.