private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); SoapElementAttribute mySoapElement = new SoapElementAttribute(); mySoapElement.ElementName = "xxxx"; soapAtts.SoapElement = mySoapElement; mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", soapAtts); // Override the IgnoreThis property. SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute(); soapAtts = new SoapAttributes(); soapAtts.SoapIgnore = false; mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", soapAtts); // Override the GroupType enumeration. soapAtts = new SoapAttributes(); SoapEnumAttribute xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "Over1000"; soapAtts.SoapEnum = xSoapEnum; // Add the SoapAttributes to the // mySoapAttributeOverridesrides object. mySoapAttributeOverrides.Add(typeof(GroupType), "A", soapAtts); // Create second enumeration and add it. soapAtts = new SoapAttributes(); xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "ZeroTo1000"; soapAtts.SoapEnum = xSoapEnum; mySoapAttributeOverrides.Add(typeof(GroupType), "B", soapAtts); // Override the Group type. soapAtts = new SoapAttributes(); SoapTypeAttribute soapType = new SoapTypeAttribute(); soapType.TypeName = "Team"; soapAtts.SoapType = soapType; mySoapAttributeOverrides.Add(typeof(Group), soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return(ser); }
public void NameDefault() { SoapEnumAttribute attr = new SoapEnumAttribute(); Assert.AreEqual(string.Empty, attr.Name, "#1"); attr.Name = null; Assert.AreEqual(string.Empty, attr.Name, "#2"); }
/// <include file='doc\SoapAttributes.uex' path='docs/doc[@for="SoapAttributes.SoapAttributes1"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public SoapAttributes(ICustomAttributeProvider provider) { object[] attrs = provider.GetCustomAttributes(false); for (int i = 0; i < attrs.Length; i++) { if (attrs[i] is SoapIgnoreAttribute || attrs[i] is ObsoleteAttribute) { _soapIgnore = true; break; } else if (attrs[i] is SoapElementAttribute) { _soapElement = (SoapElementAttribute)attrs[i]; } else if (attrs[i] is SoapAttributeAttribute) { _soapAttribute = (SoapAttributeAttribute)attrs[i]; } else if (attrs[i] is SoapTypeAttribute) { _soapType = (SoapTypeAttribute)attrs[i]; } else if (attrs[i] is SoapEnumAttribute) { _soapEnum = (SoapEnumAttribute)attrs[i]; } else if (attrs[i] is DefaultValueAttribute) { _soapDefaultValue = ((DefaultValueAttribute)attrs[i]).Value; } } if (_soapIgnore) { _soapElement = null; _soapAttribute = null; _soapType = null; _soapEnum = null; _soapDefaultValue = null; } }
private void SerializeOverride(string fileName) { SoapAttributeOverrides soapOver = new SoapAttributeOverrides(); SoapAttributes SoapAtts = new SoapAttributes(); // Add a SoapEnumAttribute for the GroupType.A enumerator. // Instead of 'A' it will be "West". SoapEnumAttribute soapEnum = new SoapEnumAttribute("West"); // Override the "A" enumerator. SoapAtts.SoapEnum = soapEnum; soapOver.Add(typeof(GroupType), "A", SoapAtts); // Add another SoapEnumAttribute for the GroupType.B enumerator. // Instead of //B// it will be "East". SoapAtts = new SoapAttributes(); soapEnum = new SoapEnumAttribute(); soapEnum.Name = "East"; SoapAtts.SoapEnum = soapEnum; soapOver.Add(typeof(GroupType), "B", SoapAtts); // Create an XmlSerializer used for overriding. XmlTypeMapping map = new SoapReflectionImporter(soapOver). ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(map); Group myGroup = new Group(); myGroup.GroupName = ".NET"; myGroup.Grouptype = GroupType.B; // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(fileName); ser.Serialize(writer, myGroup); writer.Close(); }