private void WriteFaultCode(XmlDictionaryWriter writer, EnvelopeVersion version, FaultCode code) { if (version == EnvelopeVersion.Soap11) { writer.WriteStartElement("", "faultcode", version.Namespace); if (code.Namespace.Length > 0) { writer.WriteXmlnsAttribute("a", code.Namespace); } writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); } else // Soap12 { writer.WriteStartElement("Code", version.Namespace); writer.WriteStartElement("Value", version.Namespace); if (code.Namespace.Length > 0) { writer.WriteXmlnsAttribute("a", code.Namespace); } writer.WriteQualifiedName(code.Name, code.Namespace); if (code.SubCode != null) { WriteFaultCode(writer, version, code.SubCode); } writer.WriteEndElement(); writer.WriteEndElement(); } }
private void WriteFaultCode(XmlDictionaryWriter writer, EnvelopeVersion version, FaultCode code, bool sub) { if (version == EnvelopeVersion.Soap11) { writer.WriteStartElement("", "faultcode", String.Empty); if (code.Namespace.Length > 0 && String.IsNullOrEmpty(writer.LookupPrefix(code.Namespace))) { writer.WriteXmlnsAttribute("a", code.Namespace); } writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); } else // Soap12 { writer.WriteStartElement(sub ? "Subcode" : "Code", version.Namespace); writer.WriteStartElement("Value", version.Namespace); if (code.Namespace.Length > 0 && String.IsNullOrEmpty(writer.LookupPrefix(code.Namespace))) { writer.WriteXmlnsAttribute("a", code.Namespace); } writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); if (code.SubCode != null) { WriteFaultCode(writer, version, code.SubCode, true); } writer.WriteEndElement(); } }
public void WriteTypedValues() { MemoryStream ms = new MemoryStream(); var dic = new XmlDictionary(); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, dic, null); w.WriteStartElement("root"); w.WriteXmlnsAttribute("x", "urn:x"); w.WriteXmlnsAttribute("xx", "urn:xx"); w.WriteValue((byte)5); w.WriteValue((short)893); w.WriteValue((int)37564); w.WriteValue((long)141421356); w.WriteValue((long)5670000000); w.WriteValue((float)1.7320508); w.WriteValue((double)2.2360679); w.WriteValue((decimal)3.141592); w.WriteValue(new DateTime(2000, 1, 2, 3, 4, 5)); w.WriteValue(new XmlDictionary().Add("xxx")); // from different dictionary -> string output, not index output // w.WriteValue ((object) null); ANE w.WriteValue(1); w.WriteQualifiedName(dic.Add("local"), dic.Add("urn:x")); w.WriteQualifiedName(dic.Add("local"), dic.Add("urn:xx")); // QName tag is not used, since the prefix is more than 1 byte w.Close(); Assert.AreEqual(typed_values, ms.ToArray()); }
public void WriteQualifiedName() { xw.WriteStartElement(null, "test", null); xw.WriteAttributeString("xmlns", "me", null, "http://localhost/"); xw.WriteQualifiedName("bob", "http://localhost/"); xw.WriteEndElement(); Assert.AreEqual("<test xmlns:me='http://localhost/'>me:bob</test>", Output); }
private void WriteAttributeQualifiedNameValue(XmlDictionaryString name, XmlDictionaryString ns) { if (dictionaryWriter == null) { writer.WriteQualifiedName(name.Value, ns.Value); } else { dictionaryWriter.WriteQualifiedName(name, ns); } }
protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion) { writer.WriteStartElement(this.Name, this.Namespace); writer.WriteXmlnsAttribute(null, this.notUnderstoodNs); writer.WriteStartAttribute("qname"); writer.WriteQualifiedName(this.notUnderstoodName, this.notUnderstoodNs); writer.WriteEndAttribute(); }
protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion) { writer.WriteStartElement(Name, Namespace); writer.WriteXmlnsAttribute(null, notUnderstoodNs); writer.WriteStartAttribute(Message12Strings.QName); writer.WriteQualifiedName(notUnderstoodName, notUnderstoodNs); writer.WriteEndAttribute(); }
public void Serialize(Type type, object graph) { if (graph == null) { writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true"); } else { Type actualType = graph.GetType(); SerializationMap map = types.FindUserMap(actualType); // For some collection types, the actual type does not matter. So get nominal serialization type instead. // (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.) if (map == null) { actualType = types.GetSerializedType(actualType); map = types.FindUserMap(actualType); } // If it is still unknown, then register it. if (map == null) { types.Add(actualType); map = types.FindUserMap(actualType); } if (actualType != type && (map == null || map.OutputXsiType)) { QName qname = types.GetXmlName(actualType); string name = qname.Name; string ns = qname.Namespace; if (qname == QName.Empty) { name = XmlConvert.EncodeLocalName(actualType.Name); ns = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace; } else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace) { ns = XmlSchema.Namespace; } if (writer.LookupPrefix(ns) == null) // it goes first (extraneous, but it makes att order compatible) { writer.WriteXmlnsAttribute(null, ns); } writer.WriteStartAttribute("type", XmlSchema.InstanceNamespace); writer.WriteQualifiedName(name, ns); writer.WriteEndAttribute(); } QName predef = KnownTypeCollection.GetPredefinedTypeName(actualType); if (predef != QName.Empty) { SerializePrimitive(type, graph, predef); } else { map.Serialize(graph, this); } } }
private void WriteTo11(XmlDictionaryWriter writer) { string name; string str2; writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace); writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace); FaultCode subCode = this.Code; if (subCode.SubCode != null) { subCode = subCode.SubCode; } if (subCode.IsSenderFault) { name = "Client"; } else if (subCode.IsReceiverFault) { name = "Server"; } else { name = subCode.Name; } if (subCode.IsPredefinedFault) { str2 = "http://schemas.xmlsoap.org/soap/envelope/"; } else { str2 = subCode.Namespace; } if (writer.LookupPrefix(str2) == null) { writer.WriteAttributeString("xmlns", "a", "http://www.w3.org/2000/xmlns/", str2); } writer.WriteQualifiedName(name, str2); writer.WriteEndElement(); FaultReasonText text = this.Reason.Translations[0]; writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace); if (text.XmlLang.Length > 0) { writer.WriteAttributeString("xml", "lang", "http://www.w3.org/XML/1998/namespace", text.XmlLang); } writer.WriteString(text.Text); writer.WriteEndElement(); if (this.Actor.Length > 0) { writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, this.Actor); } if (this.HasDetail) { this.OnWriteDetail(writer, EnvelopeVersion.Soap11); } writer.WriteEndElement(); }
public static void C14NWriterNegativeTests() { const string TestTypeNullStream = "Null Stream"; const string TestTypeNullElementInIncludePrefixes = "Null element in IncludePrefixes"; TestCase tc = TestConfigHelper.GetTest("C14NWriterNegativeTests"); foreach (var input in tc.Inputs) { string testType = input.Arguments[0].Value; if (testType == TestTypeNullStream) { try { //creating XmlC14NWriter with a null stream; XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(Stream.Null); writer.StartCanonicalization(null, true, new string[] { "p1", "p2" }); Assert.False(true, "Error, creating XmlC14NWriter with a null stream should have thrown!"); } catch (Exception ex) { //System.ArgumentNullException: {{ResLookup:;Value cannot be null.;ManagedString;mscorlib.dll;mscorlib;ArgumentNull_Generic}} Assert.Equal(input.Arguments[1].Value, ex.GetType().FullName); } } else if (testType == TestTypeNullElementInIncludePrefixes) { MemoryStream ms1 = new MemoryStream(); MemoryStream ms2 = new MemoryStream(); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms1); try { //Creating the C14N writer with null elements in the IncludePrefixes array; writer.WriteStartElement("p2", "Root", "http://namespace"); //Starting the canonicalization; writer.StartCanonicalization(ms2, true, new string[] { "p1", null, "p2" }); //Writing the first element in the C14N writer; writer.WriteStartElement("Wee"); //Writing some content to finish the start element. Last chance for it to throw writer.WriteQualifiedName("foo", "http://namespace"); writer.WriteEndElement(); writer.EndCanonicalization(); writer.WriteEndElement(); Assert.False(true, "Error, creating XmlC14NWriter with null elements in include prefixes should have thrown!"); } catch (Exception ex) { //System.ArgumentException: {{ResLookup:;The inclusive namespace prefix collection cannot contain null as one of the items.;ManagedString;System.Runtime.Serialization.dll;System.Runtime.Serialization;InvalidInclusivePrefixListCollection}} Assert.Equal(input.Arguments[1].Value, ex.GetType().FullName); } } else { throw new ArgumentException("Don't know how to run test " + testType); } } }
private void WriteFaultCode12Driver(XmlDictionaryWriter writer, FaultCode faultCode, EnvelopeVersion version) { writer.WriteStartElement(XD.Message12Dictionary.FaultValue, version.DictionaryNamespace); string name; if (faultCode.IsSenderFault) { name = version.SenderFaultName; } else if (faultCode.IsReceiverFault) { name = version.ReceiverFaultName; } else { name = faultCode.Name; } string ns; if (faultCode.IsPredefinedFault) { ns = version.Namespace; } else { ns = faultCode.Namespace; } string prefix = writer.LookupPrefix(ns); if (prefix == null) { writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns); } writer.WriteQualifiedName(name, ns); writer.WriteEndElement(); if (faultCode.SubCode != null) { writer.WriteStartElement(XD.Message12Dictionary.FaultSubcode, version.DictionaryNamespace); WriteFaultCode12Driver(writer, faultCode.SubCode, version); writer.WriteEndElement(); } }
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) { writer.WriteStartElement("r", "FaultCode", this.Namespace); writer.WriteXmlnsAttribute(null, this.Namespace); writer.WriteQualifiedName(this.Subcode, this.Namespace); writer.WriteEndElement(); bool flag = base.ReliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11; if (flag) { writer.WriteStartElement("r", XD.Message12Dictionary.FaultDetail, this.DictionaryNamespace); } this.fault.WriteDetail(writer); if (flag) { writer.WriteEndElement(); } }
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) { writer.WriteStartElement(WsrmFeb2005Strings.Prefix, WsrmFeb2005Strings.FaultCode, this.Namespace); writer.WriteXmlnsAttribute(null, this.Namespace); writer.WriteQualifiedName(this.Subcode, this.Namespace); writer.WriteEndElement(); bool wsrm11 = this.ReliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11; if (wsrm11) { writer.WriteStartElement(WsrmFeb2005Strings.Prefix, XD.Message12Dictionary.FaultDetail, this.DictionaryNamespace); } this.fault.WriteDetail(writer); if (wsrm11) { writer.WriteEndElement(); } }
private void WriteFaultCode12Driver(XmlDictionaryWriter writer, FaultCode faultCode, EnvelopeVersion version) { string senderFaultName; string str2; writer.WriteStartElement(XD.Message12Dictionary.FaultValue, version.DictionaryNamespace); if (faultCode.IsSenderFault) { senderFaultName = version.SenderFaultName; } else if (faultCode.IsReceiverFault) { senderFaultName = version.ReceiverFaultName; } else { senderFaultName = faultCode.Name; } if (faultCode.IsPredefinedFault) { str2 = version.Namespace; } else { str2 = faultCode.Namespace; } if (writer.LookupPrefix(str2) == null) { writer.WriteAttributeString("xmlns", "a", "http://www.w3.org/2000/xmlns/", str2); } writer.WriteQualifiedName(senderFaultName, str2); writer.WriteEndElement(); if (faultCode.SubCode != null) { writer.WriteStartElement(XD.Message12Dictionary.FaultSubcode, version.DictionaryNamespace); this.WriteFaultCode12Driver(writer, faultCode.SubCode, version); writer.WriteEndElement(); } }
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) { writer.WriteStartElement(Addressing10Strings.ProblemHeaderQName, this.Namespace); writer.WriteQualifiedName(_invalidHeaderName, this.Namespace); writer.WriteEndElement(); }
public override void WriteStartObject( XmlDictionaryWriter writer, object graph) { Type rootType = type; if (root_name.Value == "") { throw new InvalidDataContractException("Type '" + type.ToString() + "' cannot have a DataContract attribute Name set to null or empty string."); } if (graph == null) { if (names_filled) { writer.WriteStartElement(root_name.Value, root_ns.Value); } else { writer.WriteStartElement(root_name, root_ns); } writer.WriteAttributeString("i", "nil", XmlSchema.InstanceNamespace, "true"); return; } QName instName = null; QName root_qname = known_types.GetQName(rootType); QName graph_qname = known_types.GetQName(graph.GetType()); known_types.Add(graph.GetType()); if (names_filled) { writer.WriteStartElement(root_name.Value, root_ns.Value); } else { writer.WriteStartElement(root_name, root_ns); } if (root_ns.Value != root_qname.Namespace) { if (root_qname.Namespace != KnownTypeCollection.MSSimpleNamespace) { writer.WriteXmlnsAttribute(null, root_qname.Namespace); } } if (root_qname == graph_qname) { if (root_qname.Namespace != KnownTypeCollection.MSSimpleNamespace && !rootType.IsEnum) { //FIXME: Hack, when should the "i:type" be written? //Not used in case of enums writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace); } return; } /* Different names */ known_types.Add(rootType); instName = KnownTypeCollection.GetPredefinedTypeName(graph.GetType()); if (instName == QName.Empty) { /* Not a primitive type */ instName = graph_qname; } else { /* FIXME: Hack, .. see test WriteObject7 () */ instName = new QName(instName.Name, XmlSchema.Namespace); } // output xsi:type as rootType is not equivalent to the graph's type. writer.WriteStartAttribute("i", "type", XmlSchema.InstanceNamespace); writer.WriteQualifiedName(instName.Name, instName.Namespace); writer.WriteEndAttribute(); }
public static void TestC14NInclusivePrefixes() { TestCase tc = TestConfigHelper.GetTest("TestC14NInclusivePrefixes"); int count = 0; foreach (var input in tc.Inputs) { count++; string rwTypeStr = input.Arguments[0].Value; ReaderWriterFactory.ReaderWriterType rwType = (ReaderWriterFactory.ReaderWriterType)Enum.Parse(typeof(ReaderWriterFactory.ReaderWriterType), rwTypeStr, true); Encoding encoding = Encoding.GetEncoding(input.Arguments[1].Value); bool mustSupportV14N = input.Arguments[2].Value.ToLower() == "true"; MemoryStream ms = new MemoryStream(); XmlWriter w = ReaderWriterFactory.CreateXmlWriter(rwType, ms, encoding); XmlDictionaryWriter writer = w as XmlDictionaryWriter; if (writer == null) { writer = XmlDictionaryWriter.CreateDictionaryWriter(w); } if (!writer.CanCanonicalize) { Assert.False(mustSupportV14N, string.Format("Error, writer {0},{1} should support C14N, but it doesn't!", rwTypeStr, encoding.ToString())); continue; } string myDefaultNamespace = "http://mynamespace"; string myNamespace1 = "http://mynamespace1"; string myNamespace2 = "http://mynamespace2"; string myNamespace3 = "http://mynamespace3"; string myNamespace4 = "http://mynamespace4"; writer.WriteStartElement("Root"); writer.WriteXmlnsAttribute("p1", myNamespace1); writer.WriteAttributeString("p1", "a", null, "b"); writer.WriteStartElement("", "Element1", myDefaultNamespace); writer.WriteAttributeString("p3", "c", myNamespace3, "d"); writer.WriteStartElement("Element2"); MemoryStream canonicalStream = new MemoryStream(); writer.StartCanonicalization(canonicalStream, false, new string[] { "p3", "p2", "p1", "" }); writer.WriteStartElement("pre", "Element3", myNamespace2); writer.WriteAttributeString("pre2", "attrName", myNamespace4, "attrValue"); writer.WriteStartElement("Element4", ""); writer.WriteStartAttribute("attr1"); writer.WriteQualifiedName("foo", myNamespace1); writer.WriteEndAttribute(); writer.WriteStartAttribute("attr2"); writer.WriteQualifiedName("bar", myNamespace3); writer.WriteEndAttribute(); writer.WriteString("Hello world"); writer.WriteEndElement(); // Element4 writer.WriteEndElement(); // pre:Element3 writer.EndCanonicalization(); writer.WriteEndElement(); // Element2 writer.WriteEndElement(); // Element1 writer.WriteEndElement(); // Root writer.Flush(); byte[] canonicalDoc = canonicalStream.ToArray(); byte[] fullDoc = ms.ToArray(); writer.Close(); // Finished creating the document XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform(); transform.InclusiveNamespacesPrefixList = "p3 p2 p1 #default"; transform.LoadInput(new MemoryStream(canonicalDoc)); Stream transformedOutput = transform.GetOutput(typeof(Stream)) as Stream; byte[] outputFromSecurity = StreamToByteArray(transformedOutput); //Finished creating the doc from the security class Helper.DumpToFile(fullDoc); Helper.DumpToFile(canonicalDoc); Helper.DumpToFile(outputFromSecurity); Assert.True(Enumerable.SequenceEqual(outputFromSecurity, canonicalDoc), $"TestC14NInclusivePrefixes test variation #{count} failed"); } }
public void Serialize(Type type, object graph) { if (graph == null) { writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true"); } #if !MOONLIGHT else if (type == typeof(XmlElement)) { ((XmlElement)graph).WriteTo(Writer); } else if (type == typeof(XmlNode [])) { foreach (var xn in (XmlNode [])graph) { xn.WriteTo(Writer); } } #endif else { QName resolvedQName = null; if (resolver != null) { XmlDictionaryString rname, rns; if (resolver.TryResolveType(graph != null ? graph.GetType() : typeof(object), type, default_resolver, out rname, out rns)) { resolvedQName = new QName(rname.Value, rns.Value); } } Type actualType = graph.GetType(); SerializationMap map; map = types.FindUserMap(actualType); // For some collection types, the actual type does not matter. So get nominal serialization type instead. // (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.) if (map == null) { // FIXME: not sure if type.IsInterface is the correct condition to determine whether items are serialized with i:type or not. (e.g. bug #675144 server response). actualType = types.GetSerializedType(type.IsInterface ? type : actualType); map = types.FindUserMap(actualType); } // If it is still unknown, then register it. if (map == null) { types.Add(actualType); map = types.FindUserMap(actualType); } if (actualType != type && (map == null || map.OutputXsiType)) { QName qname = resolvedQName ?? types.GetXmlName(actualType); string name = qname.Name; string ns = qname.Namespace; if (qname == QName.Empty) { name = XmlConvert.EncodeLocalName(actualType.Name); ns = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace; } else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace) { ns = XmlSchema.Namespace; } if (writer.LookupPrefix(ns) == null) // it goes first (extraneous, but it makes att order compatible) { writer.WriteXmlnsAttribute(null, ns); } writer.WriteStartAttribute("i", "type", XmlSchema.InstanceNamespace); writer.WriteQualifiedName(name, ns); writer.WriteEndAttribute(); } QName predef = KnownTypeCollection.GetPredefinedTypeName(actualType); if (predef != QName.Empty) { SerializePrimitive(type, graph, predef); } else { map.Serialize(graph, this); } } }
private void WriteTo11(XmlDictionaryWriter writer) { writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace); writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace); FaultCode faultCode = Code; if (faultCode.SubCode != null) { faultCode = faultCode.SubCode; } string name; if (faultCode.IsSenderFault) { name = "Client"; } else if (faultCode.IsReceiverFault) { name = "Server"; } else { name = faultCode.Name; } string ns; if (faultCode.IsPredefinedFault) { ns = Message11Strings.Namespace; } else { ns = faultCode.Namespace; } string prefix = writer.LookupPrefix(ns); if (prefix == null) { writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns); } writer.WriteQualifiedName(name, ns); writer.WriteEndElement(); FaultReasonText translation = Reason.Translations[0]; writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace); if (translation.XmlLang.Length > 0) { writer.WriteAttributeString("xml", "lang", XmlUtil.XmlNs, translation.XmlLang); } writer.WriteString(translation.Text); writer.WriteEndElement(); if (Actor.Length > 0) { writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, Actor); } if (HasDetail) { OnWriteDetail(writer, EnvelopeVersion.Soap11); } writer.WriteEndElement(); }
protected override void OnWriteDetailContents(XmlDictionaryWriter writer) { writer.WriteStartElement(Addressing10Strings.ProblemHeaderQName, AddressingVersion.WSAddressing10.Namespace); writer.WriteQualifiedName(_invalidHeaderName, AddressingVersion.WSAddressing10.Namespace); writer.WriteEndElement(); }