How To Force WCF Service To Use XmlSerializer Instead Of The DataContractSerializer



TODO:

Have you ever wanted to return Attributes, or have further control over serialization when using WCF?  By default, it uses the DataContractSerializer, but you can tell it to use the XmlSerializer.  

 

SOLUTION:

1.  Ensure you have your interface marked with [ServiceContract]

2.  Add XmlSerializerFormat to your method

 [ServiceContract]

 publicinterfaceIMyService


 [WebGet(UriTemplate = "employee")]

 [OperationContract]

 [XmlSerializerFormat]

 Response GetEmployees();

 

Now your WCF call will use the XmlSerializer rather than the DataContractSerializer.


NOTES:

There are no notes on this topic.

Serialize A List<T> Produces Flat Results C#



TODO:

Have you serialzied an object that had a List<T> as a property, and the list is flattened when it gets to XML?

 

SOLUTION:

This occurs if you have the property marked as [XmlElement("name")].  instead mark the property as [XmlArray("name")] and you will get parent child nodes for your List<T>

 

NOTES:

There are no notes on this topic

How To Serialize An Object Using C#



TODO:

Have you ever wanted to Serialize an object using C#?  The method I created below, will allow you to pass in an object and turn it into an XML Document.

 

SOLUTION:

/// <summary>
/// 
/// </summary>
/// <param name="myObject"></param>
/// <returns></returns>
public static XmlDocument MakeXmlDocumentFromObject(Object myObject)
{
     UTF8Encoding encoding = new UTF8Encoding();
     XmlDocument returnObject = new XmlDocument();

     try
     {
          //create our serializer
          XmlSerializer xmlSerial = new XmlSerializer(myObject.GetType());

          //get it....
          using (MemoryStream memStream = new MemoryStream())
          {
               //serialize the object
               xmlSerial.Serialize(memStream, myObject);

               //load the XML now, and we will return it....
               returnObject.LoadXml(encoding.GetString(memStream.GetBuffer()));
          }

          return returnObject;
     }
     catch (Exception)
     {
          return null;
     }
}

 

NOTES:

There are no notes on this subject.

How To Find a Node At Any Level In An XML Document



TODO:

Have you ever wanted to find a Node, named "CustomerName" for example anywhere in an XML Document?

 

SOLUTION:

 

XmlNodeList customerNameNodes = MyXMLDocument.SelectNodes("//CustomerName");

 

NOTES:

There are no notes on this topic.

How To Copy All Nodes Of An XML Document And Add An Atrribute To Each Node



TODO:

Have you ever wanted to copy all nodes of an xml document, and add an attribute to each node?

 

SOLUTION:

 

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="dirty">true</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

 

 

NOTES:

I had a problem, in that I had an Object that had over 50+ properties.  When I took in XML and deserialized it, I did not know what nodes were supplied.  I wanted to allow folks to pass in just the parts of the document they wanted to update, but had no notion of "dirty".  This will add a "dirty" attribute to all nodes, which allows me to easily grab only the dirty ones, and update my database accordingly.