本文整理汇总了C#中System.Xml.Serialization.XmlAttributeOverrides类的典型用法代码示例。如果您正苦于以下问题:C# XmlAttributeOverrides类的具体用法?C# XmlAttributeOverrides怎么用?C# XmlAttributeOverrides使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlAttributeOverrides类属于System.Xml.Serialization命名空间,在下文中一共展示了XmlAttributeOverrides类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ButtonSendErrorReport_Click
private async void ButtonSendErrorReport_Click(object sender, RoutedEventArgs e)
{
var ex = Error.ToExceptionless();
ex.SetUserDescription(string.Empty, NoteTextBox.Text);
ex.AddObject(HurricaneSettings.Instance.Config, "HurricaneSettings", null, null, true);
if (HurricaneSettings.Instance.IsLoaded)
{
using (var sw = new StringWriter())
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides(); //DONT serialize the passwords and send them to me!
XmlAttributes attribs = new XmlAttributes {XmlIgnore = true};
attribs.XmlElements.Add(new XmlElementAttribute("Passwords"));
overrides.Add(typeof(ConfigSettings), "Passwords", attribs);
var xmls = new XmlSerializer(typeof(ConfigSettings), overrides);
xmls.Serialize(sw, HurricaneSettings.Instance.Config);
var doc = new XmlDocument();
doc.LoadXml(sw.ToString());
ex.SetProperty("HurricaneSettings", JsonConvert.SerializeXmlNode(doc));
}
}
ex.Submit();
((Button)sender).IsEnabled = false;
StatusProgressBar.IsIndeterminate = true;
await ExceptionlessClient.Default.ProcessQueueAsync();
StatusProgressBar.IsIndeterminate = false;
Application.Current.Shutdown();
}
示例2: button1_Click
//SerialSample4\form1.cs
private void button1_Click(object sender, System.EventArgs e)
{
//create the XmlAttributes boject
XmlAttributes attrs=new XmlAttributes();
//add the types of the objects that will be serialized
attrs.XmlElements.Add(new XmlElementAttribute("Book",typeof(BookProduct)));
attrs.XmlElements.Add(new XmlElementAttribute("Product",typeof(Product)));
XmlAttributeOverrides attrOver=new XmlAttributeOverrides();
//add to the attributes collection
attrOver.Add(typeof(Inventory),"InventoryItems",attrs);
//create the Product and Book objects
Product newProd=new Product();
BookProduct newBook=new BookProduct();
newProd.ProductID=100;
newProd.ProductName="Product Thing";
newProd.SupplierID=10;
newBook.ProductID=101;
newBook.ProductName="How to Use Your New Product Thing";
newBook.SupplierID=10;
newBook.ISBN="123456789";
Product[] addProd={newProd,newBook};
Inventory inv=new Inventory();
inv.InventoryItems=addProd;
TextWriter tr=new StreamWriter("..\\..\\..\\inventory.xml");
XmlSerializer sr=new XmlSerializer(typeof(Inventory),attrOver);
sr.Serialize(tr,inv);
tr.Close();
MessageBox.Show("Serialized!");
}
示例3: getRequestContent
public byte[] getRequestContent( string doctype, string root, Type type, object obj)
{
XmlSerializer serializer = null;
if (root == null)
{
//... root element will be the object type name
serializer = new XmlSerializer(type);
}
else
{
//... root element set explicitely
var xattribs = new XmlAttributes();
var xroot = new XmlRootAttribute(root);
xattribs.XmlRoot = xroot;
var xoverrides = new XmlAttributeOverrides();
xoverrides.Add(type, xattribs);
serializer = new XmlSerializer(type, xoverrides);
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
settings.OmitXmlDeclaration = false;
settings.Encoding = new UTF8Encoding(false/*no BOM*/, true/*throw if input illegal*/);
XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
xmlNameSpace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlNameSpace.Add("noNamespaceSchemaLocation", m_schemadir + "/" + doctype + "." + m_schemaext);
StringWriter sw = new StringWriter();
XmlWriter xw = XmlWriter.Create( sw, settings);
xw.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
serializer.Serialize(xw, obj, xmlNameSpace);
return settings.Encoding.GetBytes( sw.ToString());
}
示例4: LoadStateFromFile
public static PersistentVM LoadStateFromFile(string filePath)
{
if (!File.Exists(filePath))
{
throw new ArgumentException(Resources.MissingPersistentVMFile, "filePath");
}
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes ignoreAttrib = new XmlAttributes();
ignoreAttrib.XmlIgnore = true;
overrides.Add(typeof(DataVirtualHardDisk), "MediaLink", ignoreAttrib);
overrides.Add(typeof(DataVirtualHardDisk), "SourceMediaLink", ignoreAttrib);
overrides.Add(typeof(OSVirtualHardDisk), "MediaLink", ignoreAttrib);
overrides.Add(typeof(OSVirtualHardDisk), "SourceImageName", ignoreAttrib);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(PersistentVM), overrides, new Type[] { typeof(NetworkConfigurationSet) }, null, null);
PersistentVM role = null;
using (var stream = new FileStream(filePath, FileMode.Open))
{
role = serializer.Deserialize(stream) as PersistentVM;
}
return role;
}
示例5: SameEmptyObjectTwice
public void SameEmptyObjectTwice()
{
// the same object should most certainly
// result in the same signature, even when it's empty
XmlAttributeOverrides ov = new XmlAttributeOverrides();
ThumbprintHelpers.SameThumbprint(ov, ov);
}
示例6: Serialize
/// <summary>
/// Serializes an object into an XML document
/// </summary>
/// <param name="obj">object to serialize</param>
/// <param name="rootAttribute">root attribute to use</param>
/// <param name="namespacePrefixes">namespace prefixes</param>
/// <returns>a string that contains the XML document</returns>
public static string Serialize(object obj, XmlRootAttribute rootAttribute, params XmlQualifiedName[] namespacePrefixes)
{
if (obj == null)
{
return null;
}
using (var textWriter = new StringWriterUTF8())
{
var type = obj.GetType();
var xmlAttributeOverrides = new XmlAttributeOverrides();
if (rootAttribute != null)
{
var xmlAttributes = new XmlAttributes();
xmlAttributes.XmlRoot = rootAttribute;
xmlAttributeOverrides.Add(type, xmlAttributes);
}
using (var xmWriter = XmlWriter.Create(textWriter, new XmlWriterSettings() { OmitXmlDeclaration = true }))
{
var namespaces = new XmlSerializerNamespaces();
if (namespacePrefixes != null)
{
foreach (var ns in namespacePrefixes)
{
namespaces.Add(ns.Name, ns.Namespace);
}
}
new XmlSerializer(type, xmlAttributeOverrides).Serialize(xmWriter, obj, namespaces);
}
return textWriter.ToString();
}
}
示例7: getRequestContent
public static byte[] getRequestContent(string doctype, string root, Type type, object obj)
{
var xattribs = new XmlAttributes();
var xroot = new XmlRootAttribute(root);
xattribs.XmlRoot = xroot;
var xoverrides = new XmlAttributeOverrides();
//... have to use XmlAttributeOverrides because .NET insists on the object name as root element name otherwise ([XmlRoot(..)] has no effect)
xoverrides.Add(type, xattribs);
XmlSerializer serializer = new XmlSerializer(type, xoverrides);
StringWriter sw = new StringWriter();
XmlWriterSettings wsettings = new XmlWriterSettings();
wsettings.OmitXmlDeclaration = false;
wsettings.Encoding = new UTF8Encoding();
XmlWriter xw = XmlWriter.Create(sw, wsettings);
xw.WriteProcessingInstruction("xml", "version='1.0' standalone='no'");
//... have to write header by hand (OmitXmlDeclaration=false has no effect)
xw.WriteDocType(root, null, doctype + ".sfrm", null);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
//... trick to avoid printing of xmlns:xsi xmlns:xsd attributes of the root element
serializer.Serialize(xw, obj, ns);
return sw.ToArray();
}
示例8: LoadStateFromFile
public static PersistentVM LoadStateFromFile(string filePath)
{
PersistentVM persistentVM;
if (File.Exists(filePath))
{
XmlAttributeOverrides xmlAttributeOverride = new XmlAttributeOverrides();
XmlAttributes xmlAttribute = new XmlAttributes();
xmlAttribute.XmlIgnore = true;
xmlAttributeOverride.Add(typeof(DataVirtualHardDisk), "MediaLink", xmlAttribute);
xmlAttributeOverride.Add(typeof(DataVirtualHardDisk), "SourceMediaLink", xmlAttribute);
xmlAttributeOverride.Add(typeof(OSVirtualHardDisk), "MediaLink", xmlAttribute);
xmlAttributeOverride.Add(typeof(OSVirtualHardDisk), "SourceImageName", xmlAttribute);
Type[] typeArray = new Type[1];
typeArray[0] = typeof(NetworkConfigurationSet);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(PersistentVM), xmlAttributeOverride, typeArray, null, null);
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
persistentVM = xmlSerializer.Deserialize(fileStream) as PersistentVM;
}
return persistentVM;
}
else
{
throw new ArgumentException("The file to load the role does not exist", "filePath");
}
}
示例9: OnCreateXmlSerializer
protected virtual void OnCreateXmlSerializer(XmlAttributeOverrides overrides, List<Type> additionalTypes, List<Type> additionalControls)
{
if (GetType().Assembly != typeof(IWindowlessControl).Assembly)
{
AddAssemblyControlsToList(GetType().Assembly, additionalControls);
}
}
示例10: GetOverridesSignature
/// <summary>
/// Creates a signature for the passed in XmlAttributeOverrides
/// </summary>
/// <param name="overrides"></param>
/// <returns>An instance indpendent signature of the XmlAttributeOverrides</returns>
public static string GetOverridesSignature(XmlAttributeOverrides overrides)
{
// GetHashCode looks at something other than the content
// of XmlOverrideAttributes and comes up with different
// hash values for two instances that would produce
// the same XML is the were applied to the XmlSerializer.
// Therefore I need to do something more intelligent
// to normalize the content of XmlAttributeOverrides
// or extract a thumbpront that only accounts for the
// attributes in XmlAttributeOverrides.
// The main problems is that
// I can only access the hashtables that store the
// overriding attributes through reflection, i.e.
// I can't run the XmlSerializerCache in a partially
// trusted security context.
//
// Also, the extra computation to create a purely
// content-based thumbprint not offset the savings.
//
// If none if these were an issue I'd simply say:
// return overrides.GetHashCode().ToString();
string thumbPrint = null;
if (null != overrides)
{
thumbPrint = XmlAttributeOverridesThumbprinter.GetThumbprint(overrides);
}
return thumbPrint;
}
示例11: DifferentThumbprint
internal static void DifferentThumbprint(XmlAttributeOverrides ov1, XmlAttributeOverrides ov2)
{
string print1 = XmlAttributeOverridesThumbprinter.GetThumbprint(ov1);
string print2 = XmlAttributeOverridesThumbprinter.GetThumbprint(ov2);
Assert.IsFalse(print1 == print2);
}
示例12: Create
public static XmlAttributeOverrides Create(Type objectType)
{
XmlAttributeOverrides xOver = null;
if (!table.TryGetValue(objectType, out xOver))
{
// Create XmlAttributeOverrides object.
xOver = new XmlAttributeOverrides();
/* Create an XmlTypeAttribute and change the name of the XML type. */
XmlTypeAttribute xType = new XmlTypeAttribute();
xType.TypeName = objectType.Name;
// Set the XmlTypeAttribute to the XmlType property.
XmlAttributes attrs = new XmlAttributes();
attrs.XmlType = xType;
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(objectType, attrs);
table.MergeSafe(objectType, xOver);
}
return xOver;
}
示例13: GetExportedDefinitionsOverrides
public static XmlAttributeOverrides GetExportedDefinitionsOverrides(XmlAttributeOverrides DefinitionsOverrides)
{
var _container = PluginContainer.GetOvalCompositionContainer();
var testTypes = _container.GetExports<TestType>().Select(exp => exp.Value.GetType());
var objectTypes = _container.GetExports<ObjectType>().Select(exp => exp.Value.GetType());
var stateTypes = _container.GetExports<StateType>().Select(exp => exp.Value.GetType());
XmlAttributes testAttributes = new XmlAttributes();
foreach (var testType in testTypes)
{
var xmlAttrs = (testType.GetCustomAttributes(typeof(XmlRootAttribute), false) as XmlRootAttribute[]).SingleOrDefault();
testAttributes.XmlArrayItems.Add(new XmlArrayItemAttribute(xmlAttrs.ElementName, testType) { Namespace = xmlAttrs.Namespace });
}
DefinitionsOverrides.Add(typeof(oval_definitions), "tests", testAttributes);
XmlAttributes objectAttributes = new XmlAttributes();
foreach (var objectType in objectTypes)
{
var xmlAttrs = (objectType.GetCustomAttributes(typeof(XmlRootAttribute), false) as XmlRootAttribute[]).SingleOrDefault();
objectAttributes.XmlArrayItems.Add(new XmlArrayItemAttribute(xmlAttrs.ElementName, objectType) { Namespace = xmlAttrs.Namespace });
}
DefinitionsOverrides.Add(typeof(oval_definitions), "objects", objectAttributes);
XmlAttributes stateAttributes = new XmlAttributes();
foreach (var stateType in stateTypes)
{
var xmlAttrs = (stateType.GetCustomAttributes(typeof(XmlRootAttribute), false) as XmlRootAttribute[]).SingleOrDefault();
stateAttributes.XmlArrayItems.Add(new XmlArrayItemAttribute(xmlAttrs.ElementName, stateType) { Namespace = xmlAttrs.Namespace });
}
DefinitionsOverrides.Add(typeof(oval_definitions), "states", stateAttributes);
return DefinitionsOverrides;
}
示例14: SerializeToXml
private static string SerializeToXml(TransportMessage transportMessage)
{
var overrides = new XmlAttributeOverrides();
var attrs = new XmlAttributes {XmlIgnore = true};
// Exclude non-serializable members
overrides.Add(typeof (TransportMessage), "Body", attrs);
overrides.Add(typeof (TransportMessage), "ReplyToAddress", attrs);
overrides.Add(typeof (TransportMessage), "Headers", attrs);
var sb = new StringBuilder();
var xws = new XmlWriterSettings { Encoding = Encoding.Unicode };
var xw = XmlWriter.Create(sb, xws);
var xs = new XmlSerializer(typeof (TransportMessage), overrides);
xs.Serialize(xw, transportMessage);
var xdoc = XDocument.Parse(sb.ToString());
var body = new XElement("Body");
var cdata = new XCData(Encoding.UTF8.GetString(transportMessage.Body));
body.Add(cdata);
xdoc.SafeElement("TransportMessage").Add(body);
sb.Clear();
var sw = new StringWriter(sb);
xdoc.Save(sw);
return sb.ToString();
}
示例15: XmlSerializer
/// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace) {
XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
for (int i = 0; i < extraTypes.Length; i++)
importer.IncludeType(extraTypes[i]);
tempAssembly = GenerateTempAssembly(importer.ImportTypeMapping(type, root));
this.events.sender = this;
}