Ejemplo n.º 1
0
        // ---------------- Functions ----------------

        public static XmlDocument ToXml(this LogBook logbook)
        {
            XmlDocument doc = new XmlDocument();

            // Create declaration.
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            // Add declaration to document.
            XmlElement?root = doc.DocumentElement;

            doc.InsertBefore(dec, root);

            XmlElement logbookNode = doc.CreateElement(XmlElementName);
            {
                XmlAttribute versionAttribute = doc.CreateAttribute(VersionAttributeName);
                versionAttribute.Value = XmlVersion.ToString();
                logbookNode.Attributes.Append(versionAttribute);
            }

            List <Log> logList = logbook.ToList();

            foreach (Log log in logList)
            {
                log.ToXml(doc, logbookNode);
            }

            doc.InsertAfter(logbookNode, dec);

            return(doc);
        }
Ejemplo n.º 2
0
        private void ExportISerializableDataContract(ClassDataContract dataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType();

            type.Name = dataContract.StableName.Name;
            schema.Items.Add(type);
            XmlElement?genericInfoElement = null;

            if (dataContract.UnderlyingType.IsGenericType)
            {
                genericInfoElement = ExportGenericInfo(dataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }

            XmlElement?isValueTypeElement = null;

            if (dataContract.BaseContract != null)
            {
                _ = CreateTypeContent(type, dataContract.BaseContract.StableName, schema);
            }
            else
            {
                schema.Namespaces.Add(Globals.SerPrefixForSchema, Globals.SerializationNamespace);
                type.Particle = ISerializableSequence;
                XmlSchemaAttribute iSerializableFactoryTypeAttribute = ISerializableFactoryTypeAttribute;
                type.Attributes.Add(iSerializableFactoryTypeAttribute);
                SchemaHelper.AddSchemaImport(ISerializableFactoryTypeAttribute.RefName.Namespace, schema);
                if (dataContract.IsValueType)
                {
                    isValueTypeElement = GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(dataContract.IsValueType), schema);
                }
            }
            type.Annotation = GetSchemaAnnotation(genericInfoElement, ExportSurrogateData(dataContract), isValueTypeElement);
        }
Ejemplo n.º 3
0
 internal XDRSchema(DataSet ds, bool fInline)
 {
     _schemaUri  = string.Empty;
     _schemaName = string.Empty;
     _schemaRoot = null;
     _ds         = ds;
 }
Ejemplo n.º 4
0
        private static void WriteIXmlSerializable(XmlWriterDelegator xmlWriter, object obj, XmlSerializableWriter xmlSerializableWriter)
        {
            xmlSerializableWriter.BeginWrite(xmlWriter.Writer, obj);
            IXmlSerializable?xmlSerializable = obj as IXmlSerializable;

            if (xmlSerializable != null)
            {
                xmlSerializable.WriteXml(xmlSerializableWriter);
            }
            else
            {
                XmlElement?xmlElement = obj as XmlElement;
                if (xmlElement != null)
                {
                    xmlElement.WriteTo(xmlSerializableWriter);
                }
                else
                {
                    XmlNode[]? xmlNodes = obj as XmlNode[];
                    if (xmlNodes != null)
                    {
                        foreach (XmlNode xmlNode in xmlNodes)
                        {
                            xmlNode.WriteTo(xmlSerializableWriter);
                        }
                    }
                    else
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.UnknownXmlType, DataContract.GetClrTypeFullName(obj.GetType()))));
                    }
                }
            }
            xmlSerializableWriter.EndWrite();
        }
Ejemplo n.º 5
0
        private void CloseWithAppendAttribute()
        {
            XmlElement?elem = _start as XmlElement;

            Debug.Assert(elem != null);
            XmlAttributeCollection attrs = elem.Attributes;

            for (int i = 0; i < _fragment.Count; i++)
            {
                XmlAttribute?attr = _fragment[i] as XmlAttribute;
                Debug.Assert(attr != null);
                int offset = attrs.FindNodeOffsetNS(attr);
                if (offset != -1 &&
                    ((XmlAttribute)attrs.nodes[offset]).Specified)
                {
                    throw new XmlException(SR.Xml_DupAttributeName, attr.Prefix.Length == 0 ? attr.LocalName : $"{attr.Prefix}:{attr.LocalName}");
                }
            }
            for (int i = 0; i < _fragment.Count; i++)
            {
                XmlAttribute?attr = _fragment[i] as XmlAttribute;
                Debug.Assert(attr != null);
                attrs.Append(attr);
            }
        }
Ejemplo n.º 6
0
        internal void ResetOwnerElementInElementIdAttrMap(string oldInnerText)
        {
            XmlElement?ownerElement = OwnerElement;

            if (ownerElement != null)
            {
                ownerElement.Attributes.ResetParentInElementIdAttrMap(oldInnerText, InnerText);
            }
        }
Ejemplo n.º 7
0
        // This method is copied from System.Xml.Linq.ElementWriter.WriteElement but adapted to DOM
        private static void WriteElementTo(XmlWriter writer, XmlElement el)
        {
            XmlElement?e    = el;
            XmlNode    root = e;
            XmlNode?   n    = e;

            while (true)
            {
                e = n as XmlElement;
                // Only use the inlined write logic for XmlElement, not for derived classes
                if (e != null && e.GetType() == typeof(XmlElement))
                {
                    // Write the element
                    e.WriteStartElement(writer);
                    // Write the element's content
                    if (e.IsEmpty)
                    {
                        // No content; use a short end element <a />
                        writer.WriteEndElement();
                    }
                    else if (e._lastChild == null)
                    {
                        // No actual content; use a full end element <a></a>
                        writer.WriteFullEndElement();
                    }
                    else
                    {
                        // There are child node(s); move to first child
                        n = e.FirstChild;
                        Debug.Assert(n != null);
                        continue;
                    }
                }
                else
                {
                    // Use virtual dispatch (might recurse)
                    n.WriteTo(writer);
                }

                // Go back to the parent after writing the last child
                while (n != root && n == n.ParentNode !.LastChild)
                {
                    n = n.ParentNode;
                    Debug.Assert(n != null);
                    writer.WriteFullEndElement();
                }

                if (n == root)
                {
                    break;
                }

                n = n.NextSibling;
                Debug.Assert(n != null);
            }
        }
Ejemplo n.º 8
0
        public static XmlElement GetOrCreate(this XmlElement element, string name, string namespaveUri)
        {
            XmlElement?child = element[name, namespaveUri];

            if (child == null)
            {
                child = element.OwnerDocument.CreateElement(name, namespaveUri);
                element.AppendChild(child);
            }
            return(child);
        }
Ejemplo n.º 9
0
        internal void ResetParentInElementIdAttrMap(string oldVal, string newVal)
        {
            XmlElement?parentElem = parent as XmlElement;

            Debug.Assert(parentElem != null);
            XmlDocument?doc = parent.OwnerDocument;

            Debug.Assert(doc != null);
            doc.RemoveElementWithId(oldVal, parentElem); //add the element into the hashtable
            doc.AddElementWithId(newVal, parentElem);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Gets the summary portion of a type's documenation or returns an empty string if not available
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetSummary(this Type type)
        {
            XmlElement?element    = type.GetDocumentation();
            XmlNode?   summaryElm = element?.SelectSingleNode("summary");

            if (summaryElm == null)
            {
                return("");
            }
            return(summaryElm.InnerText.Trim());
        }
Ejemplo n.º 11
0
        public override void WriteFullEndElement()
        {
            VerifyState(Method.WriteFullEndElement);
            XmlElement?elem = _write as XmlElement;

            if (elem == null)
            {
                throw new InvalidOperationException();
            }

            elem.IsEmpty = false;
            _write       = elem.ParentNode;
        }
Ejemplo n.º 12
0
        internal bool PrepareOwnerElementInElementIdAttrMap()
        {
            XmlDocument ownerDocument = OwnerDocument;

            if (ownerDocument.DtdSchemaInfo != null)
            { // DTD exists
                XmlElement?ownerElement = OwnerElement;
                if (ownerElement != null)
                {
                    return(ownerElement.Attributes.PrepareParentInElementIdAttrMap(Prefix, LocalName));
                }
            }

            return(false);
        }
Ejemplo n.º 13
0
        private static void CreateDependencyElement(XmlDocument doc, XmlElement?group1, XmlElement?group2, string ver, string name)
        {
            var depEntry = doc.CreateElement("dependency");
            var attr_id  = doc.CreateAttribute("id");
            var attr_ver = doc.CreateAttribute("version");
            var attr_x   = doc.CreateAttribute("exclude");

            attr_id.Value  = name;
            attr_ver.Value = ver;
            attr_x.Value   = "Build,Analyzers";
            depEntry.Attributes.Append(attr_id);
            depEntry.Attributes.Append(attr_ver);
            depEntry.Attributes.Append(attr_x);
            group1?.AppendChild(depEntry);
            group2?.AppendChild(depEntry.Clone());
        }
Ejemplo n.º 14
0
        internal void LoadSchema(XmlElement schemaRoot, DataSet ds)
        {
            if (schemaRoot == null)
            {
                return;
            }

            _schemaRoot = schemaRoot;
            _ds         = ds;
            _schemaName = schemaRoot.GetAttribute(Keywords.NAME);


            _schemaUri = string.Empty;
            Debug.Assert(FEqualIdentity(schemaRoot, Keywords.XDR_SCHEMA, Keywords.XDRNS), "Illegal node");

            // Get Locale and CaseSensitive properties

            if (_schemaName == null || _schemaName.Length == 0)
            {
                _schemaName = "NewDataSet";
            }

            ds.Namespace = _schemaUri;

            // Walk all the top level Element tags.
            for (XmlNode?n = schemaRoot.FirstChild; n != null; n = n.NextSibling)
            {
                if (!(n is XmlElement))
                {
                    continue;
                }

                XmlElement child = (XmlElement)n;

                if (FEqualIdentity(child, Keywords.XDR_ELEMENTTYPE, Keywords.XDRNS))
                {
                    HandleTable(child);
                }
            }

            _schemaName = XmlConvert.DecodeName(_schemaName);
            if (ds.Tables[_schemaName] == null)
            {
                ds.DataSetName = _schemaName;
            }
        }
Ejemplo n.º 15
0
        private void AddAttribute(XmlAttribute attr, XmlNode?parent)
        {
            if (parent == null)
            {
                _fragment.Add(attr);
            }
            else
            {
                XmlElement?elem = parent as XmlElement;
                if (elem == null)
                {
                    throw new InvalidOperationException();
                }

                elem.Attributes.Append(attr);
            }
        }
Ejemplo n.º 16
0
        //remove the parent element node from the map when the ID attribute is removed
        internal void RemoveParentFromElementIdAttrMap(XmlAttribute attr)
        {
            XmlElement?parentElem = parent as XmlElement;

            if (parentElem != null)
            {
                if (parent.OwnerDocument == null)
                {
                    return;
                }

                XmlName?attrname = parent.OwnerDocument.GetIDInfoByElement(parentElem.XmlName);
                if (attrname != null && attrname.Prefix == attr.XmlName.Prefix && attrname.LocalName == attr.XmlName.LocalName)
                {
                    parent.OwnerDocument.RemoveElementWithId(attr.Value, parentElem); //remove the element from the hashtable
                }
            }
        }
Ejemplo n.º 17
0
        //insert the parent element node into the map
        internal void InsertParentIntoElementIdAttrMap(XmlAttribute attr)
        {
            XmlElement?parentElem = parent as XmlElement;

            if (parentElem != null)
            {
                if (parent.OwnerDocument == null)
                {
                    return;
                }

                XmlName?attrname = parent.OwnerDocument.GetIDInfoByElement(parentElem.XmlName);
                if (attrname != null && attrname.Prefix == attr.XmlName.Prefix && attrname.LocalName == attr.XmlName.LocalName)
                {
                    parent.OwnerDocument.AddElementWithId(attr.Value, parentElem); //add the element into the hashtable
                }
            }
        }
Ejemplo n.º 18
0
        internal bool PrepareParentInElementIdAttrMap(string attrPrefix, string attrLocalName)
        {
            XmlElement?parentElem = parent as XmlElement;

            Debug.Assert(parentElem != null);
            XmlDocument?doc = parent.OwnerDocument;

            Debug.Assert(doc != null);
            //The returned attrname if not null is the name with namespaceURI being set to string.Empty
            //Because DTD doesn't support namespaceURI so all comparisons are based on no namespaceURI (string.Empty);
            XmlName?attrname = doc.GetIDInfoByElement(parentElem.XmlName);

            if (attrname != null && attrname.Prefix == attrPrefix && attrname.LocalName == attrLocalName)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 19
0
        public void PushElement(string?elementName)
        {
            _element = new XmlElement()
            {
                ElementName = elementName
            };

            if (_elements.Count == 0)
            {
                _root = _element;
            }
            else
            {
                var parent = _elements.Peek();
                parent.AddChild(_element);
            }

            _elements.Push(_element);
        }
Ejemplo n.º 20
0
        private void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema)
        {
            XmlSchemaSimpleType type = new XmlSchemaSimpleType();

            type.Name = enumDataContract.StableName.Name;
            // https://github.com/dotnet/runtime/issues/41448 - enumDataContract.BaseContractName is always null, but this method is not reachable
            Debug.Assert(enumDataContract.BaseContractName != null, "BaseContractName is always null, but this method is not reachable. Suppressing compiler error.");
            XmlElement?actualTypeElement = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : ExportActualType(enumDataContract.BaseContractName);

            type.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(enumDataContract));
            schema.Items.Add(type);

            XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();

            restriction.BaseTypeName = StringQualifiedName;
            SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema);
            if (enumDataContract.Values != null)
            {
                for (int i = 0; i < enumDataContract.Values.Count; i++)
                {
                    XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet();
                    facet.Value = enumDataContract.Members[i].Name;
                    if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i))
                    {
                        facet.Annotation = GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema);
                    }
                    restriction.Facets.Add(facet);
                }
            }
            if (enumDataContract.IsFlags)
            {
                XmlSchemaSimpleTypeList list          = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType     anonymousType = new XmlSchemaSimpleType();
                anonymousType.Content = restriction;
                list.ItemType         = anonymousType;
                type.Content          = list;
            }
            else
            {
                type.Content = restriction;
            }
        }
Ejemplo n.º 21
0
 private static XmlElement?ImportAnnotation(XmlSchemaAnnotation?annotation, XmlQualifiedName annotationQualifiedName)
 {
     if (annotation != null && annotation.Items != null && annotation.Items.Count > 0 && annotation.Items[0] is XmlSchemaAppInfo)
     {
         XmlSchemaAppInfo appInfo = (XmlSchemaAppInfo)annotation.Items[0];
         XmlNode?[]?      markup  = appInfo.Markup;
         if (markup != null)
         {
             for (int i = 0; i < markup.Length; i++)
             {
                 XmlElement?annotationElement = markup[i] as XmlElement;
                 if (annotationElement != null && annotationElement.LocalName == annotationQualifiedName.Name && annotationElement.NamespaceURI == annotationQualifiedName.Namespace)
                 {
                     return(annotationElement);
                 }
             }
         }
     }
     return(null);
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Loads extensions from the configuration file
        /// </summary>
        /// <param name="id">Extension ID</param>
        /// <param name="logger">Logger</param>
        /// <exception cref="Exception">Loading failure</exception>
        private void LoadExtension(string id)
        {
            if (Extensions.ContainsKey(id))
            {
                throw new ExtensionException(id, "Extension has been already loaded");
            }

            XmlNode?   extensionsConfig = ServiceDescriptor.ExtensionsConfiguration;
            XmlElement?configNode       = (extensionsConfig != null) ? extensionsConfig.SelectSingleNode("extension[@id='" + id + "'][1]") as XmlElement : null;

            if (configNode == null)
            {
                throw new ExtensionException(id, "Cannot get the configuration entry");
            }

            var descriptor = WinSWExtensionDescriptor.FromXml(configNode);

            if (descriptor.Enabled)
            {
                IWinSWExtension extension = CreateExtensionInstance(descriptor.Id, descriptor.ClassName);
                extension.Descriptor = descriptor;
                try
                {
                    extension.Configure(ServiceDescriptor, configNode);
                }
                catch (Exception ex)
                { // Consider any unexpected exception as fatal
                    Log.Fatal("Failed to configure the extension " + id, ex);
                    throw ex;
                }

                Extensions.Add(id, extension);
                Log.Info("Extension loaded: " + id);
            }
            else
            {
                Log.Warn("Extension is disabled: " + id);
            }
        }
Ejemplo n.º 23
0
        internal static XmlQualifiedName ImportActualType(XmlSchemaAnnotation?annotation, XmlQualifiedName defaultTypeName, XmlQualifiedName typeName)
        {
            XmlElement?actualTypeElement = ImportAnnotation(annotation, ActualTypeAnnotationName);

            if (actualTypeElement == null)
            {
                return(defaultTypeName);
            }

            XmlNode?nameAttribute = actualTypeElement.Attributes.GetNamedItem(ImportGlobals.ActualTypeNameAttribute);

            if (nameAttribute?.Value == null)
            {
                throw ExceptionUtil.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.AnnotationAttributeNotFound, ActualTypeAnnotationName.Name, typeName.Name, typeName.Namespace, ImportGlobals.ActualTypeNameAttribute)));
            }
            XmlNode?nsAttribute = actualTypeElement.Attributes.GetNamedItem(ImportGlobals.ActualTypeNamespaceAttribute);

            if (nsAttribute?.Value == null)
            {
                throw ExceptionUtil.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.AnnotationAttributeNotFound, ActualTypeAnnotationName.Name, typeName.Name, typeName.Namespace, ImportGlobals.ActualTypeNamespaceAttribute)));
            }
            return(new XmlQualifiedName(nameAttribute.Value, nsAttribute.Value));
        }
Ejemplo n.º 24
0
            public bool Equals(XmlElement?x, XmlElement?y)
            {
                if (x == null || y == null)
                {
                    return(false);
                }
                if (x.NamespaceURI != y.NamespaceURI || x.Name != y.Name || x.InnerText != y.InnerText)
                {
                    return(false);
                }

                // ReSharper disable once InvokeAsExtensionMethod
                bool attributesEqual = EnumerableExtensions.UnsequencedEquals(
                    x.Attributes.OfType <XmlAttribute>().ToArray(),
                    y.Attributes.OfType <XmlAttribute>().ToArray(),
                    comparer: XmlAttributeComparer.Instance);
                bool elementsEqual = EnumerableExtensions.SequencedEquals(
                    x.ChildNodes.OfType <XmlElement>().ToArray(),
                    y.ChildNodes.OfType <XmlElement>().ToArray(),
                    comparer: Instance);

                return(attributesEqual && elementsEqual);
            }
Ejemplo n.º 25
0
#pragma warning restore 618

        private XmlParserContext GetContext(XmlNode?node)
        {
            string?         lang      = null;
            XmlSpace        spaceMode = XmlSpace.None;
            XmlDocumentType?docType   = _doc !.DocumentType;
            string          baseURI   = _doc.BaseURI;
            //constructing xmlnamespace
            HashSet <string>    prefixes = new HashSet <string>();
            XmlNameTable        nt       = _doc.NameTable;
            XmlNamespaceManager mgr      = new XmlNamespaceManager(nt);
            bool bHasDefXmlnsAttr        = false;

            // Process all xmlns, xmlns:prefix, xml:space and xml:lang attributes
            while (node != null && node != _doc)
            {
                XmlElement?element = node as XmlElement;
                if (element != null && element.HasAttributes)
                {
                    mgr.PushScope();
                    foreach (XmlAttribute attr in element.Attributes)
                    {
                        if (attr.Prefix == _doc.strXmlns && !prefixes.Contains(attr.LocalName))
                        {
                            // Make sure the next time we will not add this prefix
                            prefixes.Add(attr.LocalName);
                            mgr.AddNamespace(attr.LocalName, attr.Value);
                        }
                        else if (!bHasDefXmlnsAttr && attr.Prefix.Length == 0 && attr.LocalName == _doc.strXmlns)
                        {
                            // Save the case xmlns="..." where xmlns is the LocalName
                            mgr.AddNamespace(string.Empty, attr.Value);
                            bHasDefXmlnsAttr = true;
                        }
                        else if (spaceMode == XmlSpace.None && attr.Prefix == _doc.strXml && attr.LocalName == _doc.strSpace)
                        {
                            // Save xml:space context
                            if (attr.Value == "default")
                            {
                                spaceMode = XmlSpace.Default;
                            }
                            else if (attr.Value == "preserve")
                            {
                                spaceMode = XmlSpace.Preserve;
                            }
                        }
                        else if (lang == null && attr.Prefix == _doc.strXml && attr.LocalName == _doc.strLang)
                        {
                            // Save xml:lag context
                            lang = attr.Value;
                        }
                    }
                }

                node = node.ParentNode;
            }
            return(new XmlParserContext(
                       nt,
                       mgr,
                       (docType == null) ? null : docType.Name,
                       (docType == null) ? null : docType.PublicId,
                       (docType == null) ? null : docType.SystemId,
                       (docType == null) ? null : docType.InternalSubset,
                       baseURI,
                       lang,
                       spaceMode
                       ));
        }
Ejemplo n.º 26
0
 public void PopElement()
 {
     _element = _elements.Pop();
 }
Ejemplo n.º 27
0
 // ATTENTION: GetRowFromElement( XmlElement ) and GetRowFromElement( XmlBoundElement ) should have the same functionality and side effects.
 // See this code fragment for why:
 //     XmlBoundElement be = ...;
 //     XmlElement e = be;
 //     GetRowFromElement( be ); // Calls GetRowFromElement( XmlBoundElement )
 //     GetRowFromElement( e );  // Calls GetRowFromElement( XmlElement ), in spite of e beeing an instance of XmlBoundElement
 internal DataRow?GetRowFromElement(XmlElement?e) => (e as XmlBoundElement)?.Row;
Ejemplo n.º 28
0
        private void ExportCollectionDataContract(CollectionDataContract collectionDataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType();

            type.Name = collectionDataContract.StableName.Name;
            schema.Items.Add(type);
            XmlElement?genericInfoElement = null, isDictionaryElement = null;

            if (collectionDataContract.UnderlyingType.IsGenericType && CollectionDataContract.IsCollectionDataContract(collectionDataContract.UnderlyingType))
            {
                genericInfoElement = ExportGenericInfo(collectionDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }
            if (collectionDataContract.IsDictionary)
            {
                isDictionaryElement = ExportIsDictionary();
            }
            type.Annotation = GetSchemaAnnotation(isDictionaryElement, genericInfoElement, ExportSurrogateData(collectionDataContract));

            XmlSchemaSequence rootSequence = new XmlSchemaSequence();

            XmlSchemaElement element = new XmlSchemaElement();

            element.Name            = collectionDataContract.ItemName;
            element.MinOccurs       = 0;
            element.MaxOccursString = Globals.OccursUnbounded;
            if (collectionDataContract.IsDictionary)
            {
                ClassDataContract    keyValueContract = (collectionDataContract.ItemContract as ClassDataContract) !;
                XmlSchemaComplexType keyValueType     = new XmlSchemaComplexType();
                XmlSchemaSequence    keyValueSequence = new XmlSchemaSequence();
                foreach (DataMember dataMember in keyValueContract.Members !)
                {
                    XmlSchemaElement keyValueElement = new XmlSchemaElement();
                    keyValueElement.Name = dataMember.Name;
                    SetElementType(keyValueElement, _dataContractSet.GetMemberTypeDataContract(dataMember), schema);
                    SchemaHelper.AddElementForm(keyValueElement, schema);
                    if (dataMember.IsNullable)
                    {
                        keyValueElement.IsNillable = true;
                    }
                    keyValueElement.Annotation = GetSchemaAnnotation(ExportSurrogateData(dataMember));
                    keyValueSequence.Items.Add(keyValueElement);
                }
                keyValueType.Particle = keyValueSequence;
                element.SchemaType    = keyValueType;
            }
            else
            {
                if (collectionDataContract.IsItemTypeNullable)
                {
                    element.IsNillable = true;
                }
                DataContract itemContract = _dataContractSet.GetItemTypeDataContract(collectionDataContract);
                SetElementType(element, itemContract, schema);
            }
            SchemaHelper.AddElementForm(element, schema);
            rootSequence.Items.Add(element);

            type.Particle = rootSequence;

            if (collectionDataContract.IsReference)
            {
                AddReferenceAttributes(type.Attributes, schema);
            }
        }
Ejemplo n.º 29
0
        private void ExportClassDataContract(ClassDataContract classDataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType();

            type.Name = classDataContract.StableName.Name;
            schema.Items.Add(type);
            XmlElement?genericInfoElement = null;

            if (classDataContract.UnderlyingType.IsGenericType)
            {
                genericInfoElement = ExportGenericInfo(classDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }

            XmlSchemaSequence rootSequence = new XmlSchemaSequence();

            for (int i = 0; i < classDataContract.Members !.Count; i++)
            {
                DataMember dataMember = classDataContract.Members[i];

                XmlSchemaElement element = new XmlSchemaElement();
                element.Name = dataMember.Name;
                XmlElement?  actualTypeElement  = null;
                DataContract memberTypeContract = _dataContractSet.GetMemberTypeDataContract(dataMember);
                if (CheckIfMemberHasConflict(dataMember))
                {
                    element.SchemaTypeName = AnytypeQualifiedName;
                    actualTypeElement      = ExportActualType(memberTypeContract.StableName);
                    SchemaHelper.AddSchemaImport(memberTypeContract.StableName.Namespace, schema);
                }
                else
                {
                    SetElementType(element, memberTypeContract, schema);
                }
                SchemaHelper.AddElementForm(element, schema);
                if (dataMember.IsNullable)
                {
                    element.IsNillable = true;
                }
                if (!dataMember.IsRequired)
                {
                    element.MinOccurs = 0;
                }

                element.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(dataMember), ExportEmitDefaultValue(dataMember));
                rootSequence.Items.Add(element);
            }

            XmlElement?isValueTypeElement = null;

            if (classDataContract.BaseContract != null)
            {
                XmlSchemaComplexContentExtension extension = CreateTypeContent(type, classDataContract.BaseContract.StableName, schema);
                extension.Particle = rootSequence;
                if (classDataContract.IsReference && !classDataContract.BaseContract.IsReference)
                {
                    AddReferenceAttributes(extension.Attributes, schema);
                }
            }
            else
            {
                type.Particle = rootSequence;
                if (classDataContract.IsValueType)
                {
                    isValueTypeElement = GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(classDataContract.IsValueType), schema);
                }
                if (classDataContract.IsReference)
                {
                    AddReferenceAttributes(type.Attributes, schema);
                }
            }
            type.Annotation = GetSchemaAnnotation(genericInfoElement, ExportSurrogateData(classDataContract), isValueTypeElement);
        }
Ejemplo n.º 30
0
        internal bool IsRegionRadical(XmlBoundElement rowElem)
        {
            // You must pass a row element (which s/b associated w/ a DataRow)
            Debug.Assert(rowElem.Row != null);

            if (rowElem.ElementState == ElementState.Defoliated)
            {
                return(true);
            }

            DataTable            table   = GetTableSchemaForElement(rowElem) !;
            DataColumnCollection columns = table.Columns;
            int iColumn = 0;

            // check column attributes...
            int cAttrs = rowElem.Attributes.Count;

            for (int iAttr = 0; iAttr < cAttrs; iAttr++)
            {
                XmlAttribute attr = rowElem.Attributes[iAttr];

                // only specified attributes are radical
                if (!attr.Specified)
                {
                    return(false);
                }

                // only mapped attrs are valid
                DataColumn?schema = GetColumnSchemaForNode(rowElem, attr);
                if (schema == null)
                {
                    return(false);
                }

                // check to see if column is in order
                if (!IsNextColumn(columns, ref iColumn, schema))
                {
                    return(false);
                }

                // must have exactly one text node (XmlNodeType.Text) child
                XmlNode?fc = attr.FirstChild;
                if (fc == null || fc.NodeType != XmlNodeType.Text || fc.NextSibling != null)
                {
                    return(false);
                }
            }

            // check column elements
            iColumn = 0;
            XmlNode?n = rowElem.FirstChild;

            for (; n != null; n = n.NextSibling)
            {
                // only elements can exist in radically structured data
                if (n.NodeType != XmlNodeType.Element)
                {
                    return(false);
                }
                XmlElement?e = n as XmlElement;

                // only checking for column mappings in this loop
                if (GetRowFromElement(e) != null)
                {
                    break;
                }

                // element's must have schema to be radically structured
                DataColumn?schema = GetColumnSchemaForNode(rowElem, e !);
                if (schema == null)
                {
                    return(false);
                }

                // check to see if column is in order
                if (!IsNextColumn(columns, ref iColumn, schema))
                {
                    return(false);
                }

                // must have no attributes
                if (e.HasAttributes)
                {
                    return(false);
                }

                // must have exactly one text node child
                XmlNode?fc = e.FirstChild;
                if (fc == null || fc.NodeType != XmlNodeType.Text || fc.NextSibling != null)
                {
                    return(false);
                }
            }

            // check for remaining sub-regions
            for (; n != null; n = n.NextSibling)
            {
                // only elements can exist in radically structured data
                if (n.NodeType != XmlNodeType.Element)
                {
                    return(false);
                }

                // element's must be regions in order to be radially structured
                DataRow?row = GetRowFromElement((XmlElement?)n);
                if (row == null)
                {
                    return(false);
                }
            }

            return(true);
        }