private void AddDataElementToXml(XmlTextWriter xml,
                                  DataElement element)
 {
     xml.WriteStartElement("DataElement");
     xml.WriteAttributeString("streamPosition", element.StreamPosition.ToString());
     xml.WriteElementString("Tag", element.Tag.ToString());
     xml.WriteElementString("VR", element.VR.ToString());
     xml.WriteElementString("VM",
                            element.Tag.GetDictionaryEntry().VM.ToString());
     xml.WriteElementString("Description",
                            element.Tag.GetDictionaryEntry().Description);
     xml.WriteElementString("ValueLength", element.ValueLength.ToString());
     if (element.Value.IsSequence)
     {
         xml.WriteStartElement("Sequence");
         xml.WriteAttributeString("elementCount",
                                  ((Sequence)element.Value[0]).Count.ToString());
         foreach (DataElement d in (Sequence)element.Value[0])
         {
             AddDataElementToXml(xml, d);
         }
     }
     else if (element.Value.IsNestedDataSet)
     {
         xml.WriteStartElement("NestedDataSet");
         xml.WriteAttributeString("elementCount",
                                  ((NestedDataSet)element.Value[0]).Count.ToString());
         foreach (DataElement d in (NestedDataSet)element.Value[0])
         {
             AddDataElementToXml(xml, d);
         }
     }
     else if (element.Value.IsMultiValue)
     {
         xml.WriteStartElement("MultiValue");
         xml.WriteAttributeString("count", element.Value.Count.ToString());
         object[] valueArray = element.Value.ToArray();
         for (int i = 0; i < valueArray.Length; i++)
         {
             xml.WriteStartElement("Value");
             xml.WriteAttributeString("order", i.ToString());
             AddValueToXml(xml, valueArray[i], element.Value.IsDate);
             xml.WriteEndElement();
         }
     }
     else if (element.Value.IsUid)
     {
         Uid uid = (Uid)element.Value[0];
         xml.WriteStartElement("Uid");
         xml.WriteStartElement("Value");
         AddValueToXml(xml, uid, element.Value.IsDate);
         xml.WriteEndElement();
         xml.WriteStartElement("Name");
         AddValueToXml(xml, uid.GetDictionaryEntry().Name,
                       element.Value.IsDate);
         xml.WriteEndElement();
         xml.WriteStartElement("Type");
         AddValueToXml(xml, UidType.GetName(typeof(UidType),
                                            uid.GetDictionaryEntry().Type), element.Value.IsDate);
         xml.WriteEndElement();
     }
     else
     {
         xml.WriteStartElement("Value");
         if (!element.Value.IsEmpty)
         {
             AddValueToXml(xml, element.Value[0], element.Value.IsDate);
         }
     }
     // Value
     xml.WriteEndElement();
     // DataElement
     xml.WriteEndElement();
 }