public abstract LookupPrefix ( string ns ) : string | ||
ns | string | |
return | string |
public override void WriteXmlSchemaType(XmlWriter xw, string xmlns) { xw.WriteStartElement("complexType", XmlSchemaUtil.SCHEMA_NS); if (Name != null) xw.WriteAttributeString("name", Name); xw.WriteStartElement("sequence", XmlSchemaUtil.SCHEMA_NS); foreach (MemberDef member in Members) { xw.WriteStartElement("element", XmlSchemaUtil.SCHEMA_NS); xw.WriteAttributeString("name", member.Name); TypeDef td = ParentTypeSet.GetTypeDef(member.TypeName); if (td.IsSimpleType) { xw.WriteAttributeString("type", "xs:" + td.Name); if (member.IsRequired) xw.WriteAttributeString("nillable", "false"); } else { if (string.IsNullOrEmpty(xmlns)) xw.WriteAttributeString("type", td.Name); else { string prf = xw.LookupPrefix(xmlns); xw.WriteAttributeString("type", string.Format("{0}:{1}", prf, td.Name)); } } xw.WriteAttributeString("minOccurs", member.IsRequired ? "1" : "0"); xw.WriteAttributeString("maxOccurs", member.IsArray ? "unbounded" : "1"); xw.WriteEndElement(); } xw.WriteEndElement(); xw.WriteEndElement(); }
protected override void WriteBeginElement(System.Xml.XmlWriter writer, object obj, ITypeSerializationInfo info) { string prefix = info.NamespacePrefix; if (prefix == null) { prefix = writer.LookupPrefix(info.Namespace); } else if (writer.LookupPrefix(info.Namespace) == null) { writer.WriteAttributeString("xmlns", prefix, null, info.Namespace); } string value = prefix + ":" + info.ElementName; writer.WriteAttributeString(XMLSchemaInstancePrefix, TypeField, XMLSchemaInstanceNamespace, value); }
public static string UseOpenEhrPrefix(System.Xml.XmlWriter writer) { string oePrefix = writer.LookupPrefix(OpenEhrNamespace); if (oePrefix == null) { oePrefix = "oe"; writer.WriteAttributeString("xmlns", oePrefix, null, OpenEhrNamespace); } return(oePrefix); }
public static string UseXsdPrefix(System.Xml.XmlWriter writer) { string xsdPrefix = writer.LookupPrefix(XsdNamespace); if (xsdPrefix == null) { xsdPrefix = "xsd"; writer.WriteAttributeString("xmlns", xsdPrefix, null, XsdNamespace); } return(xsdPrefix); }
public override string LookupPrefix(string ns) { if (!supports_lookup) { return(String.Empty); } try { return(writer.LookupPrefix(ns)); } catch (NotSupportedException ex) { supports_lookup = false; return(String.Empty); } catch (Exception ex) { throw; } }
internal static string ToQName(this PrintSchemaName self, XmlWriter writer) { var prefix = writer.LookupPrefix(self.NamespaceName); if (prefix == null) { throw new InternalException($"Namespace declaration not found: {self.Namespace}"); } else if (prefix == string.Empty) { return self.LocalName; } else { return prefix + ":" + self.LocalName; } }
public override string LookupPrefix(string ns) { return(writer.LookupPrefix(ns)); }
internal void WriteXml (XmlWriter writer, DiscoveryVersion version) { if (writer == null) throw new ArgumentNullException ("writer"); // standard members writer.WriteStartElement ("d", "Types", version.Namespace); int p = 0; foreach (var qname in ContractTypeNames) if (writer.LookupPrefix (qname.Namespace) == null) writer.WriteAttributeString ("xmlns", "p" + p++, "http://www.w3.org/2000/xmlns/", qname.Namespace); writer.WriteValue (ContractTypeNames); writer.WriteEndElement (); writer.WriteStartElement ("Scopes", version.Namespace); if (ScopeMatchBy != null) { writer.WriteStartAttribute ("MatchBy"); writer.WriteValue (ScopeMatchBy); writer.WriteEndAttribute (); } writer.WriteValue (Scopes); writer.WriteEndElement (); // non-standard members if (MaxResults != default_max_results) { writer.WriteStartElement ("MaxResults", SerializationNS); writer.WriteValue (MaxResults); writer.WriteEndElement (); } writer.WriteStartElement ("Duration", SerializationNS); writer.WriteValue (Duration); writer.WriteEndElement (); foreach (var ext in Extensions) ext.WriteTo (writer); }
public override string?LookupPrefix(string ns) { return(_wrapped.LookupPrefix(ns)); }
public override void WriteTo (XmlWriter w) { // some people expect the same prefix output as in input, // in the loss of performance... see bug #466423. string prefix = name.NamespaceName.Length > 0 ? w.LookupPrefix (name.Namespace.NamespaceName) : String.Empty; foreach (XAttribute a in Attributes ()) { if (a.IsNamespaceDeclaration && a.Value == name.Namespace.NamespaceName) { if (a.Name.Namespace == XNamespace.Xmlns) prefix = a.Name.LocalName; // otherwise xmlns="..." break; } } w.WriteStartElement (prefix, name.LocalName, name.Namespace.NamespaceName); foreach (XAttribute a in Attributes ()) { if (a.IsNamespaceDeclaration) { if (a.Name.Namespace == XNamespace.Xmlns) w.WriteAttributeString ("xmlns", a.Name.LocalName, XNamespace.Xmlns.NamespaceName, a.Value); else w.WriteAttributeString ("xmlns", a.Value); } else w.WriteAttributeString (a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value); } foreach (XNode node in Nodes ()) node.WriteTo (w); if (explicit_is_empty) w.WriteEndElement (); else w.WriteFullEndElement (); }
/// <summary> /// Serialize a SamlAuthorityBinding. /// </summary> /// <param name="writer">XmlWriter to serialize the SamlAuthorityBinding</param> /// <param name="authorityBinding">SamlAuthoriyBinding to be serialized.</param> protected virtual void WriteAuthorityBinding(XmlWriter writer, SamlAuthorityBinding authorityBinding) { if (writer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer"); } if (authorityBinding == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("statement"); } writer.WriteStartElement(SamlConstants.Prefix, SamlConstants.ElementNames.AuthorityBinding, SamlConstants.Namespace); string prefix = null; if (!string.IsNullOrEmpty(authorityBinding.AuthorityKind.Namespace)) { writer.WriteAttributeString(String.Empty, SamlConstants.AttributeNames.NamespaceAttributePrefix, null, authorityBinding.AuthorityKind.Namespace); prefix = writer.LookupPrefix(authorityBinding.AuthorityKind.Namespace); } writer.WriteStartAttribute(SamlConstants.AttributeNames.AuthorityKind, null); if (string.IsNullOrEmpty(prefix)) { writer.WriteString(authorityBinding.AuthorityKind.Name); } else { writer.WriteString(prefix + ":" + authorityBinding.AuthorityKind.Name); } writer.WriteEndAttribute(); writer.WriteAttributeString(SamlConstants.AttributeNames.Location, null, authorityBinding.Location); writer.WriteAttributeString(SamlConstants.AttributeNames.Binding, null, authorityBinding.Binding); writer.WriteEndElement(); }
private void InvokeMethod(XmlWriter w, string methodName) { byte[] buffer = new byte[10]; switch (methodName) { case "WriteStartDocument": w.WriteStartDocument(); break; case "WriteStartElement": w.WriteStartElement("root"); break; case "WriteEndElement": w.WriteEndElement(); break; case "WriteStartAttribute": w.WriteStartAttribute("attr"); break; case "WriteEndAttribute": w.WriteEndAttribute(); break; case "WriteCData": w.WriteCData("test"); break; case "WriteComment": w.WriteComment("test"); break; case "WritePI": w.WriteProcessingInstruction("name", "test"); break; case "WriteEntityRef": w.WriteEntityRef("e"); break; case "WriteCharEntity": w.WriteCharEntity('c'); break; case "WriteSurrogateCharEntity": w.WriteSurrogateCharEntity('\uDC00', '\uDBFF'); break; case "WriteWhitespace": w.WriteWhitespace(" "); break; case "WriteString": w.WriteString("foo"); break; case "WriteChars": char[] charArray = new char[] { 'a', 'b', 'c', 'd' }; w.WriteChars(charArray, 0, 3); break; case "WriteRaw": w.WriteRaw("<foo>bar</foo>"); break; case "WriteBase64": w.WriteBase64(buffer, 0, 9); break; case "WriteBinHex": w.WriteBinHex(buffer, 0, 9); break; case "LookupPrefix": string str = w.LookupPrefix("foo"); break; case "WriteNmToken": w.WriteNmToken("foo"); break; case "WriteName": w.WriteName("foo"); break; case "WriteQualifiedName": w.WriteQualifiedName("foo", "bar"); break; case "WriteValue": w.WriteValue(Int32.MaxValue); break; case "WriteAttributes": XmlReader xr1 = ReaderHelper.Create(new StringReader("<root attr='test'/>")); xr1.Read(); w.WriteAttributes(xr1, false); break; case "WriteNodeReader": XmlReader xr2 = ReaderHelper.Create(new StringReader("<root/>")); xr2.Read(); w.WriteNode(xr2, false); break; case "Flush": w.Flush(); break; default: CError.Equals(false, "Unexpected param in testcase: {0}", methodName); break; } }
/// <summary> /// Writes the internal data for this ContentLocatorGroup to the writer. This method /// is used by an XmlSerializer to serialize a ContentLocatorGroup. To serialize a /// ContentLocatorGroup to Xml, use an XmlSerializer. /// </summary> /// <param name="writer">the writer to write internal data to</param> /// <exception cref="ArgumentNullException">writer is null</exception> public void WriteXml(XmlWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } string prefix = writer.LookupPrefix(AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); if (prefix == null) { writer.WriteAttributeString(AnnotationXmlConstants.Prefixes.XmlnsPrefix, AnnotationXmlConstants.Prefixes.CoreSchemaPrefix, null, AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); } foreach (ContentLocatorBase locator in _locators) { if (locator != null) { AnnotationResource.ListSerializer.Serialize(writer, locator); } } }
private void WriteTypeQualifier(XmlWriter writer, ITypeSerializationInfo type) { if (type.NamespacePrefix != null) { if (writer.LookupPrefix(type.Namespace) != type.NamespacePrefix) { writer.WriteAttributeString("xmlns", type.NamespacePrefix, null, type.Namespace); } writer.WriteAttributeString(XMLSchemaInstancePrefix, TypeField, XMLSchemaInstanceNamespace, type.NamespacePrefix + ":" + type.ElementName); } else { writer.WriteStartAttribute(XMLSchemaInstancePrefix, TypeField, XMLSchemaInstanceNamespace); writer.WriteQualifiedName(type.ElementName, type.Namespace); writer.WriteEndAttribute(); } }
internal void WriteXml (XmlWriter writer, DiscoveryVersion version) { if (writer == null) throw new ArgumentNullException ("writer"); // standard members if (Address != null) Address.WriteTo (AddressingVersion.WSAddressing10, writer); writer.WriteStartElement ("d", "Types", version.Namespace); int p = 0; foreach (var qname in ContractTypeNames) if (writer.LookupPrefix (qname.Namespace) == null) writer.WriteAttributeString ("xmlns", "p" + p++, "http://www.w3.org/2000/xmlns/", qname.Namespace); writer.WriteValue (ContractTypeNames); writer.WriteEndElement (); if (Scopes.Count > 0) { writer.WriteStartElement ("Scopes", version.Namespace); writer.WriteValue (Scopes); writer.WriteEndElement (); } if (ListenUris.Count > 0) { writer.WriteStartElement ("XAddrs", version.Namespace); writer.WriteValue (ListenUris); writer.WriteEndElement (); } writer.WriteStartElement ("MetadataVersion", version.Namespace); writer.WriteValue (Version); writer.WriteEndElement (); // non-standard members foreach (var ext in Extensions) ext.WriteTo (writer); }
internal override void WriteFault(XmlWriter writer, SoapException soapException, HttpStatusCode statusCode) { if ((statusCode == HttpStatusCode.InternalServerError) && (soapException != null)) { SoapServerMessage message = base.ServerProtocol.Message; writer.WriteStartDocument(); writer.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); writer.WriteAttributeString("xmlns", "soap", null, "http://schemas.xmlsoap.org/soap/envelope/"); writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); if (base.ServerProtocol.ServerMethod != null) { SoapHeaderHandling.WriteHeaders(writer, base.ServerProtocol.ServerMethod.outHeaderSerializer, message.Headers, base.ServerProtocol.ServerMethod.outHeaderMappings, SoapHeaderDirection.Fault, base.ServerProtocol.ServerMethod.use == SoapBindingUse.Encoded, base.ServerType.serviceNamespace, base.ServerType.serviceDefaultIsEncoded, "http://schemas.xmlsoap.org/soap/envelope/"); } else { SoapHeaderHandling.WriteUnknownHeaders(writer, message.Headers, "http://schemas.xmlsoap.org/soap/envelope/"); } writer.WriteStartElement("Body", "http://schemas.xmlsoap.org/soap/envelope/"); writer.WriteStartElement("Fault", "http://schemas.xmlsoap.org/soap/envelope/"); writer.WriteStartElement("faultcode", ""); XmlQualifiedName name = TranslateFaultCode(soapException.Code); if (((name.Namespace != null) && (name.Namespace.Length > 0)) && (writer.LookupPrefix(name.Namespace) == null)) { writer.WriteAttributeString("xmlns", "q0", null, name.Namespace); } writer.WriteQualifiedName(name.Name, name.Namespace); writer.WriteEndElement(); writer.WriteStartElement("faultstring", ""); if ((soapException.Lang != null) && (soapException.Lang.Length != 0)) { writer.WriteAttributeString("xml", "lang", "http://www.w3.org/XML/1998/namespace", soapException.Lang); } writer.WriteString(base.ServerProtocol.GenerateFaultString(soapException)); writer.WriteEndElement(); string actor = soapException.Actor; if (actor.Length > 0) { writer.WriteElementString("faultactor", "", actor); } if (!(soapException is SoapHeaderException)) { if (soapException.Detail == null) { writer.WriteStartElement("detail", ""); writer.WriteEndElement(); } else { soapException.Detail.WriteTo(writer); } } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); } }
/// <summary> /// Writes the internal data for this ContentLocator to the writer. This /// method is used by an XmlSerializer to serialize a ContentLocator. Don't /// use this method directly, to serialize a ContentLocator to Xml, use an /// XmlSerializer. /// </summary> /// <param name="writer">the writer to write internal data to</param> /// <exception cref="ArgumentNullException">writer is null</exception> public void WriteXml(XmlWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } string prefix = writer.LookupPrefix(AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); if (prefix == null) { writer.WriteAttributeString(AnnotationXmlConstants.Prefixes.XmlnsPrefix, AnnotationXmlConstants.Prefixes.CoreSchemaPrefix, null, AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); } prefix = writer.LookupPrefix(AnnotationXmlConstants.Namespaces.BaseSchemaNamespace); if (prefix == null) { writer.WriteAttributeString(AnnotationXmlConstants.Prefixes.XmlnsPrefix, AnnotationXmlConstants.Prefixes.BaseSchemaPrefix, null, AnnotationXmlConstants.Namespaces.BaseSchemaNamespace); } // Write each ContentLocatorPart as its own element foreach (ContentLocatorPart part in _parts) { prefix = writer.LookupPrefix(part.PartType.Namespace); if (String.IsNullOrEmpty(prefix)) { prefix = "tmp"; } // ContentLocatorParts cannot write themselves out becuase the element // name is based on the part's type. The ContentLocatorPart instance // has no way (through normal serialization) to change the element // name it writes out at runtime. writer.WriteStartElement(prefix, part.PartType.Name, part.PartType.Namespace); // Each name/value pair for the ContentLocatorPart becomes an attribute foreach (KeyValuePair<string, string> pair in part.NameValuePairs) { writer.WriteStartElement(AnnotationXmlConstants.Elements.Item, AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); writer.WriteAttributeString(AnnotationXmlConstants.Attributes.ItemName, pair.Key); writer.WriteAttributeString(AnnotationXmlConstants.Attributes.ItemValue, pair.Value); writer.WriteEndElement(); } writer.WriteEndElement(); } }
internal override void WriteFault(XmlWriter writer, SoapException soapException, HttpStatusCode statusCode) { if (statusCode != HttpStatusCode.InternalServerError) return; if (soapException == null) return; SoapServerMessage message = ServerProtocol.Message; writer.WriteStartDocument(); writer.WriteStartElement(Soap.Prefix, Soap.Element.Envelope, Soap.Namespace); writer.WriteAttributeString("xmlns", Soap.Prefix, null, Soap.Namespace); writer.WriteAttributeString("xmlns", "xsi", null, XmlSchema.InstanceNamespace); writer.WriteAttributeString("xmlns", "xsd", null, XmlSchema.Namespace); if (ServerProtocol.ServerMethod != null) SoapHeaderHandling.WriteHeaders(writer, ServerProtocol.ServerMethod.outHeaderSerializer, message.Headers, ServerProtocol.ServerMethod.outHeaderMappings, SoapHeaderDirection.Fault, ServerProtocol.ServerMethod.use == SoapBindingUse.Encoded, ServerType.serviceNamespace, ServerType.serviceDefaultIsEncoded, Soap.Namespace); else SoapHeaderHandling.WriteUnknownHeaders(writer, message.Headers, Soap.Namespace); writer.WriteStartElement(Soap.Element.Body, Soap.Namespace); writer.WriteStartElement(Soap.Element.Fault, Soap.Namespace); writer.WriteStartElement(Soap.Element.FaultCode, ""); XmlQualifiedName code = TranslateFaultCode(soapException.Code); if (code.Namespace != null && code.Namespace.Length > 0 && writer.LookupPrefix(code.Namespace) == null) writer.WriteAttributeString("xmlns", "q0", null, code.Namespace); writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); // write faultString element with possible "lang" attribute writer.WriteStartElement(Soap.Element.FaultString, ""); if (soapException.Lang != null && soapException.Lang.Length != 0) { writer.WriteAttributeString("xml", Soap.Attribute.Lang, Soap.XmlNamespace, soapException.Lang); } writer.WriteString(ServerProtocol.GenerateFaultString(soapException)); writer.WriteEndElement(); // Only write an actor element if the actor was specified (it's optional for end-points) string actor = soapException.Actor; if (actor.Length > 0) writer.WriteElementString(Soap.Element.FaultActor, "", actor); // Only write a FaultDetail element if exception is related to the body (not a header) if (!(soapException is SoapHeaderException)) { if (soapException.Detail == null) { // writer.WriteStartElement(Soap.Element.FaultDetail, ""); writer.WriteEndElement(); } else { soapException.Detail.WriteTo(writer); } } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); }
public override void WriteTo (XmlWriter writer) { if (writer == null) throw new ArgumentNullException ("writer"); writer.WriteStartElement ("app", "categories", Version); if (writer.LookupPrefix (Namespaces.Atom10) != "a10") writer.WriteAttributeString ("xmlns", "a10", Namespaces.Xmlns, Namespaces.Atom10); // xml:lang, xml:base, term, scheme, label if (Document.Language != null) writer.WriteAttributeString ("xml", "lang", Namespaces.Xml, Document.Language); if (Document.BaseUri != null) writer.WriteAttributeString ("xml", "base", Namespaces.Xml, Document.BaseUri.ToString ()); InlineCategoriesDocument inline = Document as InlineCategoriesDocument; ReferencedCategoriesDocument referenced = Document as ReferencedCategoriesDocument; // ... no term ? if (inline != null) { if (inline.IsFixed) writer.WriteAttributeString ("fixed", "yes"); if (inline.Scheme != null) writer.WriteAttributeString ("scheme", inline.Scheme); } else if (referenced != null) { if (referenced.Link != null) writer.WriteAttributeString ("href", referenced.Link.ToString ()); } Document.WriteAttributeExtensions (writer, Version); Document.WriteElementExtensions (writer, Version); if (inline != null) WriteInlineCategoriesContent (inline, writer); // no (non-extension) contents for out-of-line category writer.WriteEndElement (); }
public override string LookupPrefix(string ns) { CheckAsync(); return(_coreWriter.LookupPrefix(ns)); }
/// <summary> /// Saves the current <see cref="AtomContent"/> to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to which you want to save.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> public void WriteTo(XmlWriter writer) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); //------------------------------------------------------------ // Write XML representation of the current instance //------------------------------------------------------------ writer.WriteStartElement("content", AtomUtility.AtomNamespace); AtomUtility.WriteCommonObjectAttributes(this, writer); if(!String.IsNullOrEmpty(this.ContentType)) { writer.WriteAttributeString("type", this.ContentType); } if (this.Source != null) { writer.WriteAttributeString("src", this.Source.ToString()); } if (String.Compare(this.ContentType, "xhtml", StringComparison.OrdinalIgnoreCase) == 0 && String.IsNullOrEmpty(writer.LookupPrefix(AtomUtility.XhtmlNamespace))) { writer.WriteAttributeString("xmlns", "xhtml", null, AtomUtility.XhtmlNamespace); } if(!String.IsNullOrEmpty(this.Content)) { if (String.Compare(this.ContentType, "xhtml", StringComparison.OrdinalIgnoreCase) == 0) { writer.WriteStartElement("div", AtomUtility.XhtmlNamespace); writer.WriteString(this.Content); writer.WriteEndElement(); } else { writer.WriteString(this.Content); } } //------------------------------------------------------------ // Write the syndication extensions of the current instance //------------------------------------------------------------ SyndicationExtensionAdapter.WriteExtensionsTo(this.Extensions, writer); writer.WriteEndElement(); }
/// <summary> /// Starts writing an element for the given DomNode</summary> /// <param name="node">DomNode to write</param> /// <param name="writer">The XML writer. See <see cref="T:System.Xml.XmlWriter"/></param> protected void WriteStartElement(DomNode node, XmlWriter writer) { // It's possible to create DomNodes with no ChildInfo, and if the DomNode is never // parented, then its ChildInfo property will still be null. We could try to search // for a compatible root element in the m_typeCollection, but that's more code. if (node.ChildInfo == null) throw new InvalidOperationException( "Please check your document's creation method to ensure that the root DomNode's" + " constructor was given a ChildInfo."); // writes the start of an element m_elementNS = m_typeCollection.TargetNamespace; int index = node.ChildInfo.Type.Name.LastIndexOf(':'); if (index >= 0) m_elementNS = node.ChildInfo.Type.Name.Substring(0, index); m_elementPrefix = string.Empty; // is this the root DomNode (the one passed to Write)? if (IsRootNode(node)) { m_elementPrefix = m_typeCollection.GetPrefix(m_elementNS); if (m_elementPrefix == null) m_elementPrefix = GeneratePrefix(m_elementNS); writer.WriteStartElement(m_elementPrefix, node.ChildInfo.Name, m_elementNS); // define the xsi namespace writer.WriteAttributeString("xmlns", "xsi", null, XmlSchema.InstanceNamespace); // define schema namespaces foreach (XmlQualifiedName name in m_typeCollection.Namespaces) if (name.Name != m_elementPrefix) // don't redefine the element namespace writer.WriteAttributeString("xmlns", name.Name, null, name.Namespace); } else { ChildInfo actualChildInfo = node.ChildInfo; var substitutionGroupRule = node.ChildInfo.Rules.OfType<SubstitutionGroupChildRule>().FirstOrDefault(); if (substitutionGroupRule != null) { var substituteChildInfo = substitutionGroupRule.Substitutions.FirstOrDefault(x => x.Type == node.Type); if (substituteChildInfo == null) { throw new InvalidOperationException("No suitable Substitution Group found for node " + node); } actualChildInfo = substituteChildInfo; m_elementNS = m_typeCollection.TargetNamespace; index = substituteChildInfo.Type.Name.LastIndexOf(':'); if (index >= 0) m_elementNS = substituteChildInfo.Type.Name.Substring(0, index); // It is possible that an element of this namspace has not // yet been written. If the lookup fails then get the prefix from // the type collection m_elementPrefix = writer.LookupPrefix(m_elementNS); if (m_elementPrefix == null) { m_elementPrefix = m_typeCollection.GetPrefix(m_elementNS); } } else { // not the root, so all schema namespaces have been defined m_elementPrefix = writer.LookupPrefix(m_elementNS); } if (m_elementPrefix == null) m_elementPrefix = GeneratePrefix(m_elementNS); writer.WriteStartElement(m_elementPrefix, actualChildInfo.Name, m_elementNS); } }
/// <summary> /// Saves the current node to the specified XmlWriter. /// </summary> /// <param name="xmlWriter"> /// The XmlWriter to which to save the current node. /// </param> public override void WriteTo(XmlWriter xmlWriter) { if (xmlWriter == null) { throw new ArgumentNullException("xmlWriter"); } if (this.XmlParsed) { //check the namespace mapping defined in this node first. because till now xmlWriter don't know the mapping defined in the current node. string prefix = LookupNamespaceLocal(this.NamespaceUri); //if not defined in the current node, try the xmlWriter if (this.Parent != null && string.IsNullOrEmpty(prefix)) { prefix = xmlWriter.LookupPrefix(this.NamespaceUri); } //if xmlWriter didn't find it, it means the node is constructed by user and is not in the tree yet //in this case, we use the predefined prefix if (string.IsNullOrEmpty(prefix)) { prefix = NamespaceIdMap.GetNamespacePrefix(this.NamespaceId); } xmlWriter.WriteStartElement(prefix, this.LocalName, this.NamespaceUri); // fix bug #225919, write out all namespace into to root WriteNamespaceAtributes(xmlWriter); this.WriteAttributesTo(xmlWriter); if (this.HasChildren || !string.IsNullOrEmpty(this.InnerText)) { this.WriteContentTo(xmlWriter); xmlWriter.WriteFullEndElement(); } else { xmlWriter.WriteEndElement(); } } else { xmlWriter.WriteRaw(this.RawOuterXml); } }
public override string?LookupPrefix(string namespaceUri) { return(_writer.LookupPrefix(namespaceUri)); }
private static void WriteTypeAndNamespace(XmlWriter output, string typeName, string clrNamespace) { if (string.IsNullOrEmpty(clrNamespace)) { output.WriteString(typeName); return; } string text = output.LookupPrefix(clrNamespace); if (text != null) { output.WriteString(text + ':' + typeName); return; } int num = clrNamespace.LastIndexOf('.'); typeName = clrNamespace.Substring(num + 1) + '.' + typeName; if (num > 0) { clrNamespace = clrNamespace.Substring(0, num); TypeNameHelper.WriteTypeAndNamespace(output, typeName, clrNamespace); return; } output.WriteString(typeName); }
public override void WriteTo(XmlWriter w) { string prefix = this.Prefix; if (!string.IsNullOrEmpty(this.NamespaceURI)) prefix = w.LookupPrefix(this.NamespaceURI) ?? this.Prefix; w.WriteStartElement(prefix, this.LocalName, this.NamespaceURI); if (this.HasAttributes) { XmlAttributePreservingWriter preservingWriter = w as XmlAttributePreservingWriter; if (preservingWriter == null || this.preservationDict == null) this.WriteAttributesTo(w); else this.WritePreservedAttributesTo(preservingWriter); } if (this.IsEmpty) { w.WriteEndElement(); } else { this.WriteContentTo(w); w.WriteFullEndElement(); } }
/// <summary> /// add the documentslist NS /// </summary> /// <param name="writer">The XmlWrite, where we want to add default namespaces to</param> protected override void AddOtherNamespaces(XmlWriter writer) { base.AddOtherNamespaces(writer); if (writer == null) { throw new ArgumentNullException("writer"); } string strPrefix = writer.LookupPrefix(DocumentslistNametable.NSDocumentslist); if (strPrefix == null) { writer.WriteAttributeString("xmlns", DocumentslistNametable.Prefix, null, DocumentslistNametable.NSDocumentslist); } }
public static void WriteRSTXml(XmlWriter writer, string elementName, object elementValue, WSTrustSerializationContext context, WSTrustConstantsAdapter trustConstants) { if (writer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer"); } if (string.IsNullOrEmpty(elementName)) { throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("elementName"); } if (context == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context"); } if (trustConstants == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("trustConstants"); } if (StringComparer.Ordinal.Equals(elementName, WSPolicyConstants.ElementNames.AppliesTo)) { EndpointReference appliesTo = elementValue as EndpointReference; WSTrustSerializationHelper.WriteAppliesTo(writer, appliesTo, trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Claims)) { writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.Claims, trustConstants.NamespaceURI); RequestClaimCollection claims = (RequestClaimCollection)elementValue; if ((claims.Dialect != null) && !UriUtil.CanCreateValidUri(claims.Dialect, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3136, trustConstants.Attributes.Dialect, trustConstants.Elements.Claims, trustConstants.NamespaceURI, claims.Dialect))); } string ns = WSTrustSerializationHelper.GetRequestClaimNamespace(claims.Dialect); string prefix = writer.LookupPrefix(ns); if (string.IsNullOrEmpty(prefix)) { prefix = WSTrustSerializationHelper.GetRequestClaimPrefix(claims.Dialect); writer.WriteAttributeString("xmlns", prefix, null, ns); } writer.WriteAttributeString(trustConstants.Attributes.Dialect, !string.IsNullOrEmpty(claims.Dialect) ? claims.Dialect : WSIdentityConstants.Dialect); foreach (RequestClaim claim in claims) { writer.WriteStartElement(prefix, WSIdentityConstants.Elements.ClaimType, ns); writer.WriteAttributeString(WSIdentityConstants.Attributes.Uri, claim.ClaimType); writer.WriteAttributeString(WSIdentityConstants.Attributes.Optional, claim.IsOptional ? "true" : "false"); if (claim.Value != null) { if (StringComparer.Ordinal.Equals(claims.Dialect, WSAuthorizationConstants.Dialect)) { writer.WriteElementString(prefix, WSAuthorizationConstants.Elements.Value, ns, claim.Value); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3257, claims.Dialect, WSAuthorizationConstants.Dialect))); } } writer.WriteEndElement(); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.ComputedKeyAlgorithm)) { WriteComputedKeyAlgorithm(writer, trustConstants.Elements.ComputedKeyAlgorithm, (string)elementValue, trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.BinaryExchange)) { WriteBinaryExchange(writer, elementValue as BinaryExchange, trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Issuer)) { WriteOnBehalfOfIssuer(writer, elementValue as EndpointReference, trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.SignWith)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.SignWith, trustConstants.NamespaceURI, (string)elementValue))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.SignWith, trustConstants.NamespaceURI, (string)elementValue); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.EncryptWith)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.EncryptWith, trustConstants.NamespaceURI, (string)elementValue))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.EncryptWith, trustConstants.NamespaceURI, (string)elementValue); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Entropy)) { Entropy entropy = elementValue as Entropy; if (entropy != null) { writer.WriteStartElement(trustConstants.Elements.Entropy, trustConstants.NamespaceURI); WriteProtectedKey(writer, entropy, context, trustConstants); writer.WriteEndElement(); } return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.KeySize)) { writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.KeySize, trustConstants.NamespaceURI, Convert.ToString(((int)elementValue), CultureInfo.InvariantCulture)); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.KeyType)) { WSTrustSerializationHelper.WriteKeyType(writer, ((string)elementValue), trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Lifetime)) { Lifetime lifeTime = (Lifetime)elementValue; WSTrustSerializationHelper.WriteLifetime(writer, lifeTime, trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.RenewTarget)) { SecurityTokenElement tokenElement = elementValue as SecurityTokenElement; if (tokenElement == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, trustConstants.Elements.RenewTarget, trustConstants.NamespaceURI, typeof(SecurityTokenElement), elementValue)); } writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.RenewTarget, trustConstants.NamespaceURI); if (tokenElement.SecurityTokenXml != null) { tokenElement.SecurityTokenXml.WriteTo(writer); } else { context.SecurityTokenHandlers.WriteToken(writer, tokenElement.GetSecurityToken()); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.OnBehalfOf)) { writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.OnBehalfOf, trustConstants.NamespaceURI); WriteTokenElement((SecurityTokenElement)elementValue, SecurityTokenHandlerCollectionManager.Usage.OnBehalfOf, context, writer); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, WSTrust14Constants.ElementNames.ActAs)) { writer.WriteStartElement(WSTrust14Constants.Prefix, WSTrust14Constants.ElementNames.ActAs, WSTrust14Constants.NamespaceURI); WriteTokenElement((SecurityTokenElement)elementValue, SecurityTokenHandlerCollectionManager.Usage.ActAs, context, writer); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.RequestType)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.RequestType, trustConstants.NamespaceURI, (string)elementValue))); } WSTrustSerializationHelper.WriteRequestType(writer, (string)elementValue, trustConstants); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.TokenType)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.TokenType, trustConstants.NamespaceURI, ((string)elementValue)))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.TokenType, trustConstants.NamespaceURI, ((string)elementValue)); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.UseKey)) { UseKey useKey = (UseKey)elementValue; if (useKey.Token == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3012))); } if (!context.SecurityTokenHandlers.CanWriteToken(useKey.Token)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3017))); } writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.UseKey, trustConstants.NamespaceURI); context.SecurityTokenHandlers.WriteToken(writer, useKey.Token); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.AuthenticationType)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.AuthenticationType, trustConstants.NamespaceURI, ((string)elementValue)))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.AuthenticationType, trustConstants.NamespaceURI, (string)elementValue); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.EncryptionAlgorithm)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.EncryptionAlgorithm, trustConstants.NamespaceURI, ((string)elementValue)))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.EncryptionAlgorithm, trustConstants.NamespaceURI, (string)elementValue); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.CanonicalizationAlgorithm)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.CanonicalizationAlgorithm, trustConstants.NamespaceURI, ((string)elementValue)))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.CanonicalizationAlgorithm, trustConstants.NamespaceURI, (string)elementValue); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.SignatureAlgorithm)) { if (!UriUtil.CanCreateValidUri((string)elementValue, UriKind.Absolute)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3135, trustConstants.Elements.SignatureAlgorithm, trustConstants.NamespaceURI, ((string)elementValue)))); } writer.WriteElementString(trustConstants.Prefix, trustConstants.Elements.SignatureAlgorithm, trustConstants.NamespaceURI, (string)elementValue); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Encryption)) { SecurityTokenElement token = (SecurityTokenElement)elementValue; writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.Encryption, trustConstants.NamespaceURI); if (token.SecurityTokenXml != null) { token.SecurityTokenXml.WriteTo(writer); } else { context.SecurityTokenHandlers.WriteToken(writer, token.GetSecurityToken()); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.ProofEncryption)) { SecurityTokenElement token = (SecurityTokenElement)elementValue; writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.ProofEncryption, trustConstants.NamespaceURI); if (token.SecurityTokenXml != null) { token.SecurityTokenXml.WriteTo(writer); } else { context.SecurityTokenHandlers.WriteToken(writer, token.GetSecurityToken()); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.DelegateTo)) { SecurityTokenElement token = (SecurityTokenElement)elementValue; writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.DelegateTo, trustConstants.NamespaceURI); if (token.SecurityTokenXml != null) { token.SecurityTokenXml.WriteTo(writer); } else { context.SecurityTokenHandlers.WriteToken(writer, token.GetSecurityToken()); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Forwardable)) { if (!(elementValue is bool)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, trustConstants.Elements.Forwardable, trustConstants.NamespaceURI, typeof(bool), elementValue)); } writer.WriteStartElement(trustConstants.Elements.Forwardable, trustConstants.NamespaceURI); writer.WriteString(XmlConvert.ToString((bool)elementValue)); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Delegatable)) { if (!(elementValue is bool)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, trustConstants.Elements.Delegatable, trustConstants.NamespaceURI, typeof(bool), elementValue)); } writer.WriteStartElement(trustConstants.Elements.Delegatable, trustConstants.NamespaceURI); writer.WriteString(XmlConvert.ToString((bool)elementValue)); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.AllowPostdating)) { writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.AllowPostdating, trustConstants.NamespaceURI); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Renewing)) { Renewing renewing = elementValue as Renewing; if (renewing == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, trustConstants.Elements.Renewing, trustConstants.NamespaceURI, typeof(Renewing), elementValue)); } writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.Renewing, trustConstants.NamespaceURI); writer.WriteAttributeString(trustConstants.Attributes.Allow, XmlConvert.ToString(renewing.AllowRenewal)); writer.WriteAttributeString(trustConstants.Attributes.OK, XmlConvert.ToString(renewing.OkForRenewalAfterExpiration)); writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.CancelTarget)) { SecurityTokenElement tokenElement = elementValue as SecurityTokenElement; if (tokenElement == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, trustConstants.Elements.CancelTarget, trustConstants.NamespaceURI, typeof(SecurityTokenElement), elementValue)); } writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.CancelTarget, trustConstants.NamespaceURI); if (tokenElement.SecurityTokenXml != null) { tokenElement.SecurityTokenXml.WriteTo(writer); } else { context.SecurityTokenHandlers.WriteToken(writer, tokenElement.GetSecurityToken()); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, trustConstants.Elements.Participants)) { Participants participants = elementValue as Participants; if (participants == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, trustConstants.Elements.Participant, trustConstants.NamespaceURI, typeof(Participants), elementValue)); } writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.Participants, trustConstants.NamespaceURI); if (participants.Primary != null) { writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.Primary, trustConstants.NamespaceURI); participants.Primary.WriteTo(writer); writer.WriteEndElement(); } foreach (EndpointReference participant in participants.Participant) { writer.WriteStartElement(trustConstants.Prefix, trustConstants.Elements.Participant, trustConstants.NamespaceURI); participant.WriteTo(writer); writer.WriteEndElement(); } writer.WriteEndElement(); return; } if (StringComparer.Ordinal.Equals(elementName, WSAuthorizationConstants.Elements.AdditionalContext)) { AdditionalContext additionalContext = elementValue as AdditionalContext; if (additionalContext == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("elementValue", SR.GetString(SR.ID3222, WSAuthorizationConstants.Elements.AdditionalContext, WSAuthorizationConstants.Namespace, typeof(AdditionalContext), elementValue)); } writer.WriteStartElement(WSAuthorizationConstants.Prefix, WSAuthorizationConstants.Elements.AdditionalContext, WSAuthorizationConstants.Namespace); foreach (ContextItem item in additionalContext.Items) { writer.WriteStartElement(WSAuthorizationConstants.Prefix, WSAuthorizationConstants.Elements.ContextItem, WSAuthorizationConstants.Namespace); writer.WriteAttributeString(WSAuthorizationConstants.Attributes.Name, item.Name.AbsoluteUri); if (item.Scope != null) { writer.WriteAttributeString(WSAuthorizationConstants.Attributes.Scope, item.Scope.AbsoluteUri); } if (item.Value != null) { writer.WriteElementString(WSAuthorizationConstants.Elements.Value, WSAuthorizationConstants.Namespace, item.Value); } writer.WriteEndElement(); } writer.WriteEndElement(); return; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WSTrustSerializationException(SR.GetString(SR.ID3013, elementName, elementValue.GetType()))); }
/// <summary> /// Writes the attributes corresponding to the node</summary> /// <param name="node">DomNode to write</param> /// <param name="writer">The XML writer. See <see cref="T:System.Xml.XmlWriter"/></param> protected virtual void WriteAttributes(DomNode node, XmlWriter writer) { // write type name if this is a polymorphic type // if this node is substitution group element then ignore type name DomNodeType type = node.Type; if (node.ChildInfo.Type != type && node.ChildInfo.Rules.OfType<SubstitutionGroupChildRule>().FirstOrDefault() == null) { string name = type.Name; int index = name.LastIndexOf(':'); if (index >= 0) { string typeName = name.Substring(index + 1, type.Name.Length - index - 1); string typeNS = name.Substring(0, index); string typePrefix = writer.LookupPrefix(typeNS); if (typePrefix == null) { typePrefix = GeneratePrefix(typeNS); writer.WriteAttributeString("xmlns", typePrefix, null, typeNS); } name = typeName; if (typePrefix != string.Empty) name = typePrefix + ":" + typeName; } writer.WriteAttributeString("xsi", "type", XmlSchema.InstanceNamespace, name); } // write attributes AttributeInfo valueAttribute = null; var attributesAsElements = new List<AttributeInfo>(); foreach (AttributeInfo attributeInfo in type.Attributes) { // if attribute is not the default, write it if (ShouldWriteAttribute(node, attributeInfo)) { if (attributeInfo.Name == string.Empty) { valueAttribute = attributeInfo; } else { if (PreserveSimpleElements) { var xmlAttributeInfo = attributeInfo as XmlAttributeInfo; if (xmlAttributeInfo != null && xmlAttributeInfo.IsElement) { attributesAsElements.Add(xmlAttributeInfo); continue; } } WriteXmlAttribute(node, attributeInfo, writer); } } } // write value if not the default if (valueAttribute != null) { string valueString = Convert(node, valueAttribute); writer.WriteString(valueString); } // write DOM attributes that were originally XML elements of a simple type foreach (AttributeInfo info in attributesAsElements) { writer.WriteStartElement(m_elementPrefix, info.Name, m_elementNS); string valueString = Convert(node, info); writer.WriteString(valueString); writer.WriteEndElement(); } }
/// <summary> /// Serializes this Resource to XML with the passed in XmlWriter. /// </summary> /// <param name="writer">the writer to serialize the Resource to</param> /// <exception cref="ArgumentNullException">writer is null</exception> public void WriteXml(XmlWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } if (String.IsNullOrEmpty(writer.LookupPrefix(AnnotationXmlConstants.Namespaces.CoreSchemaNamespace))) { writer.WriteAttributeString(AnnotationXmlConstants.Prefixes.XmlnsPrefix, AnnotationXmlConstants.Prefixes.CoreSchemaPrefix, null, AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); } writer.WriteAttributeString(AnnotationXmlConstants.Attributes.Id, XmlConvert.ToString(_id)); if (_name != null) { writer.WriteAttributeString(AnnotationXmlConstants.Attributes.ResourceName, _name); } // Use the actual field here to avoid creating the collection for no reason if (_locators != null) { foreach (ContentLocatorBase locator in _locators) { if (locator != null) { if (locator is ContentLocatorGroup) { LocatorGroupSerializer.Serialize(writer, locator); } else { ListSerializer.Serialize(writer, locator); } } } } // Use the actual field here to avoid creating the collection for no reason if (_contents != null) { foreach (XmlElement content in _contents) { if (content != null) { content.WriteTo(writer); } } } }
/// <summary> /// add the spreadsheet NS /// </summary> /// <param name="writer">The XmlWrite, where we want to add default namespaces to</param> protected override void AddOtherNamespaces(XmlWriter writer) { base.AddOtherNamespaces(writer); if (writer == null) { throw new ArgumentNullException("writer"); } string strPrefix = writer.LookupPrefix(GDataSpreadsheetsNameTable.NSGSpreadsheets); if (strPrefix == null) { writer.WriteAttributeString("xmlns", GDataSpreadsheetsNameTable.Prefix, null, GDataSpreadsheetsNameTable.NSGSpreadsheets); } }
private static void WriteFaultCodeValue(XmlWriter writer, XmlQualifiedName code, SoapFaultSubCode subcode) { if (code != null) { writer.WriteStartElement("Value", "http://www.w3.org/2003/05/soap-envelope"); if (((code.Namespace != null) && (code.Namespace.Length > 0)) && (writer.LookupPrefix(code.Namespace) == null)) { writer.WriteAttributeString("xmlns", "q0", null, code.Namespace); } writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); if (subcode != null) { writer.WriteStartElement("Subcode", "http://www.w3.org/2003/05/soap-envelope"); WriteFaultCodeValue(writer, subcode.Code, subcode.SubCode); writer.WriteEndElement(); } } }
/// <summary> /// Saves the current <see cref="AtomTextConstruct"/> to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to which you want to save.</param> /// <param name="elementName">The local name of the text construct being written.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="elementName"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="elementName"/> is an empty string.</exception> public void WriteTo(XmlWriter writer, string elementName) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNullOrEmptyString(elementName, "elementName"); //------------------------------------------------------------ // Write XML representation of the current instance //------------------------------------------------------------ writer.WriteStartElement(elementName, AtomUtility.AtomNamespace); AtomUtility.WriteCommonObjectAttributes(this, writer); if (this.TextType == AtomTextConstructType.Xhtml && String.IsNullOrEmpty(writer.LookupPrefix(AtomUtility.XhtmlNamespace))) { writer.WriteAttributeString("xmlns", "xhtml", null, AtomUtility.XhtmlNamespace); } if (this.TextType != AtomTextConstructType.None) { writer.WriteAttributeString("type", AtomTextConstruct.ConstructTypeAsString(this.TextType)); } if (this.TextType == AtomTextConstructType.Xhtml) { writer.WriteStartElement("div", AtomUtility.XhtmlNamespace); writer.WriteString(this.Content); writer.WriteEndElement(); } else { writer.WriteString(this.Content); } //------------------------------------------------------------ // Write the syndication extensions of the current instance //------------------------------------------------------------ SyndicationExtensionAdapter.WriteExtensionsTo(this.Extensions, writer); writer.WriteEndElement(); }
static void PrepareQNameString(StringBuilder listOfQNamesString, ref bool emptyNsDeclared, ref int prefixCount, XmlWriter writer, XmlQualifiedName qname) { string prefix; string encodedLocalName = XmlConvert.EncodeLocalName(qname.Name.Trim()); if (qname.Namespace.Length == 0) { if (!emptyNsDeclared) { writer.WriteAttributeString("xmlns", string.Empty, null, string.Empty); emptyNsDeclared = true; } prefix = null; } else { prefix = writer.LookupPrefix(qname.Namespace); if (prefix == null) { prefix = "dp" + prefixCount++; writer.WriteAttributeString("xmlns", prefix, null, qname.Namespace); } } if (!string.IsNullOrEmpty(prefix)) { listOfQNamesString.AppendFormat(CultureInfo.InvariantCulture, "{0}:{1}", prefix, encodedLocalName); } else { listOfQNamesString.AppendFormat(CultureInfo.InvariantCulture, "{0}", encodedLocalName); } }
public override string LookupPrefix(string namespaceUri) { return(w.LookupPrefix(namespaceUri)); }
internal override void AddSCPDDescriptionForStandardDataType(XmlWriter writer) { writer.WriteStartElement("dataType"); string prefix = writer.LookupPrefix(_dataType.SchemaURI); writer.WriteAttributeString("type", prefix + ':' + _dataType.DataTypeName); writer.WriteString("string"); writer.WriteEndElement(); // dataType }
void WriteContentTo (XmlWriter writer) { if (writer.LookupPrefix (Namespaces.Atom10) == null) writer.WriteAttributeString ("xmlns", "a10", Namespaces.Xmlns, Namespaces.Atom10); if (writer.LookupPrefix (Version) == null) writer.WriteAttributeString ("xmlns", "app", Namespaces.Xmlns, Version); // xml:lang, xml:base, workspace* if (Document.Language != null) writer.WriteAttributeString ("xml", "lang", Namespaces.Xml, Document.Language); if (Document.BaseUri != null) writer.WriteAttributeString ("xml", "base", Namespaces.Xml, Document.BaseUri.ToString ()); Document.WriteAttributeExtensions (writer, Version); Document.WriteElementExtensions (writer, Version); foreach (var ws in Document.Workspaces) { writer.WriteStartElement ("app", "workspace", Version); // xml:base, title, collection* if (ws.BaseUri != null) writer.WriteAttributeString ("xml", "base", Namespaces.Xml, ws.BaseUri.ToString ()); ws.WriteAttributeExtensions (writer, Version); ws.WriteElementExtensions (writer, Version); if (ws.Title != null) ws.Title.WriteTo (writer, "title", Namespaces.Atom10); foreach (var rc in ws.Collections) { writer.WriteStartElement ("app", "collection", Version); // accept*, xml:base, category, @href, title if (rc.BaseUri != null) writer.WriteAttributeString ("xml", "base", Namespaces.Xml, rc.BaseUri.ToString ()); if (rc.Link != null) writer.WriteAttributeString ("href", rc.Link.ToString ()); rc.WriteAttributeExtensions (writer, Version); if (rc.Title != null) rc.Title.WriteTo (writer, "title", Namespaces.Atom10); foreach (var s in rc.Accepts) { writer.WriteStartElement ("app", "accept", Version); writer.WriteString (s); writer.WriteEndElement (); } foreach (var cat in rc.Categories) cat.Save (writer); writer.WriteEndElement (); } writer.WriteEndElement (); } }
private static void WriteFaultCodeValue(XmlWriter writer, XmlQualifiedName code, SoapFaultSubCode subcode) { if (code == null) return; writer.WriteStartElement(Soap12.Element.FaultCodeValue, Soap12.Namespace); if (code.Namespace != null && code.Namespace.Length > 0 && writer.LookupPrefix(code.Namespace) == null) writer.WriteAttributeString("xmlns", "q0", null, code.Namespace); writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); // </value> if (subcode != null) { writer.WriteStartElement(Soap12.Element.FaultSubcode, Soap12.Namespace); WriteFaultCodeValue(writer, subcode.Code, subcode.SubCode); writer.WriteEndElement(); // </subcode> } }
string LookupPrefix (string ns, XmlWriter w) { string prefix = ns.Length > 0 ? w.LookupPrefix (ns) : String.Empty; foreach (XAttribute a in Attributes ()) { if (a.IsNamespaceDeclaration && a.Value == ns) { if (a.Name.Namespace == XNamespace.Xmlns) prefix = a.Name.LocalName; // otherwise xmlns="..." break; } } return prefix; }
public static bool TryWriteElementValue(this XmlWriter writer, string localName, string ns, bool?value, bool allowEmpty = false) { var prefix = writer.LookupPrefix(ns); return(writer.TryWriteElementValue(prefix, localName, ns, value, allowEmpty: allowEmpty)); }