Exemplo n.º 1
0
        private void WriteObject(object key, object obj, XmlTextWriter writer, bool isRoot)
        {
            MarkupObject markupObj  = MarkupWriter.GetMarkupObjectFor(obj);
            Type         objectType = markupObj.ObjectType;

            _namespaceCache.GetXmlNamespace(objectType);
            string ns     = _namespaceCache.GetXmlNamespace(objectType);
            string prefix = _namespaceCache.GetPrefixForNamespace(ns);

            WriteStartElement(writer, prefix, markupObj.ObjectType.Name);

            if (isRoot)
            {
                foreach (NamespaceMap map in _namespaceMaps.Values)
                {
                    if (string.IsNullOrEmpty(map.Prefix))
                    {
                        writer.WriteAttributeString("xmlns", map.XmlNamespace);
                    }
                    else
                    {
                        writer.WriteAttributeString("xmlns:" + map.Prefix, map.XmlNamespace);
                    }
                }

                if (!_namespaceMaps.ContainsKey(NamespaceCache.XamlNamespace))
                {
                    writer.WriteAttributeString("xmlns:x", NamespaceCache.XamlNamespace);
                }
            }

            if (key != null)
            {
                string keyString = key.ToString();
                if (keyString.Length > 0)
                {
                    writer.WriteAttributeString("x:Key", keyString);
                }
                else
                {
                    //TODO: key may not be a string, what about x:Type...
                    throw new NotImplementedException();
                }
            }

            List <MarkupProperty> propertyElements = new List <MarkupProperty>();
            MarkupProperty        contentProperty  = null;
            string contentString = string.Empty;

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (IsContentProperty(markupObj, markupProperty))
                {
                    contentProperty = markupProperty;
                    continue;
                }

                if (markupProperty.IsValueAsString)
                {
                    contentString = markupProperty.Value as string;
                }
                else if (!markupProperty.IsComposite)
                {
                    string temp = markupProperty.Value == null ? string.Empty : _formatterConverter.ToString(markupProperty.Value);

                    if (markupProperty.IsAttached)
                    {
                        string ns1     = _namespaceCache.GetXmlNamespace(markupProperty.DependencyProperty.OwnerType);
                        string prefix1 = _namespaceCache.GetPrefixForNamespace(ns1);
                        if (string.IsNullOrEmpty(prefix1))
                        {
                            writer.WriteAttributeString(markupProperty.Name, temp);
                        }
                        else
                        {
                            writer.WriteAttributeString(prefix1 + ":" + markupProperty.Name, temp);
                        }
                    }
                    else
                    {
                        if (markupProperty.Name == "Name" && NamespaceCache.GetAssemblyNameFromType(markupProperty.DependencyProperty.OwnerType).Equals("PresentationFramework"))
                        {
                            writer.WriteAttributeString("x:" + markupProperty.Name, temp);
                        }
                        else
                        {
                            writer.WriteAttributeString(markupProperty.Name, temp);
                        }
                    }
                }
                else if (markupProperty.Value.GetType() == typeof(NullExtension))
                {
                    writer.WriteAttributeString(markupProperty.Name, "{x:Null}");
                }
                else
                {
                    propertyElements.Add(markupProperty);
                }
            }

            if (contentProperty != null || propertyElements.Count > 0 || !string.IsNullOrEmpty(contentString))
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string ns2             = _namespaceCache.GetXmlNamespace(markupObj.ObjectType);
                    string prefix2         = _namespaceCache.GetPrefixForNamespace(ns2);
                    string propElementName = markupObj.ObjectType.Name + "." + markupProp.Name;

                    WriteStartElement(writer, prefix2, propElementName);
                    WriteChildren(writer, markupProp);
                    writer.WriteEndElement();
                }

                if (!string.IsNullOrEmpty(contentString))
                {
                    writer.WriteValue(contentString);
                }
                else if (contentProperty != null)
                {
                    if (contentProperty.Value is string)
                    {
                        writer.WriteValue(contentProperty.StringValue);
                    }
                    else
                    {
                        WriteChildren(writer, contentProperty);
                    }
                }
            }

            writer.WriteEndElement();
        }
Exemplo n.º 2
0
        private void WriteProperties(XmlTextWriter writer, MarkupObject markupObj)
        {
            List <MarkupProperty> propertyElements = new List <MarkupProperty>();
            MarkupProperty        contentProperty  = null;
            string contentString = string.Empty;

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (IsContentProperty(markupObj, markupProperty))
                {
                    contentProperty = markupProperty;
                    continue;
                }

                if (markupProperty.IsValueAsString)
                {
                    contentString = markupProperty.Value as string;
                }
                else if (!markupProperty.IsComposite)
                {
                    string temp = markupProperty.Value == null ? string.Empty : markupProperty.Value.ToString();

                    if (markupProperty.IsAttached)
                    {
                        string ns1     = _namespaceCache.GetXmlNamespace(markupProperty.DependencyProperty.OwnerType);
                        string prefix1 = _namespaceCache.GetPrefixForNamespace(ns1);
                        if (string.IsNullOrEmpty(prefix1))
                        {
                            writer.WriteAttributeString(markupProperty.Name, temp);
                        }
                        else
                        {
                            writer.WriteAttributeString(prefix1 + ":" + markupProperty.Name, temp);
                        }
                    }
                    else
                    {
                        DependencyProperty dependencyProperty = markupProperty.DependencyProperty;
                        Type ownerType = dependencyProperty == null ? null : dependencyProperty.OwnerType;
                        if (markupProperty.Name == "Name" && ownerType != null && NamespaceCache.GetAssemblyNameFromType(ownerType).Equals("PresentationFramework"))
                        {
                            writer.WriteAttributeString("x:" + markupProperty.Name, temp);
                        }
                        else
                        {
                            writer.WriteAttributeString(markupProperty.Name, temp);
                        }
                    }
                }
                else if (markupProperty.Value.GetType() == typeof(NullExtension))
                {
                    writer.WriteAttributeString(markupProperty.Name, "{x:Null}");
                }
                else
                {
                    propertyElements.Add(markupProperty);
                }
            }

            if (contentProperty != null || propertyElements.Count > 0 || !string.IsNullOrEmpty(contentString))
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string ns2             = _namespaceCache.GetXmlNamespace(markupObj.ObjectType);
                    string prefix2         = _namespaceCache.GetPrefixForNamespace(ns2);
                    string propElementName = markupObj.ObjectType.Name + "." + markupProp.Name;

                    WriteStartElement(writer, prefix2, propElementName);
                    WriteChildren(writer, markupProp);
                    writer.WriteEndElement();
                }

                if (!string.IsNullOrEmpty(contentString))
                {
                    writer.WriteValue(contentString);
                }
                else if (contentProperty != null)
                {
                    if (contentProperty.Value is string)
                    {
                        writer.WriteValue(contentProperty.StringValue);
                    }
                    else
                    {
                        WriteChildren(writer, contentProperty);
                    }
                }
            }
        }