Table of Contents

Class XmlStorage

Namespace
NanoByte.Common.Storage
Assembly
NanoByte.Common.dll

Provides easy serialization to XML files.

[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "[DynamicallyAccessedMembers] captures top-level types, library users are expected to manually root transient dependencies.")]
public static class XmlStorage
Inheritance
object
XmlStorage

Fields

XsiNamespace

The XML namespace used for XML Schema instance.

public const string XsiNamespace = "http://www.w3.org/2001/XMLSchema-instance"

Field Value

string

Properties

Overrides

Global overrides for XML serialization.

public static XmlAttributeOverrides? Overrides { get; set; }

Property Value

XmlAttributeOverrides

Remarks

Changing this value or its properties is not thread-safe. When set to a non-null value, pre-generated XML serialization assemblies (sgen) are not used.

Methods

FromXmlString<T>(string)

Loads an object from an XML string.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
public static T FromXmlString<T>(string data)

Parameters

data string

The XML string to be parsed.

Returns

T

The loaded object.

Type Parameters

T

The type of object the XML string shall be converted into.

Exceptions

InvalidDataException

A problem occurred while deserializing the XML data.

LoadXml<T>(Stream)

Loads an object from an XML file.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
public static T LoadXml<T>(Stream stream)

Parameters

stream Stream

The stream to read the encoded XML data from.

Returns

T

The loaded object.

Type Parameters

T

The type of object the XML stream shall be converted into.

Exceptions

InvalidDataException

A problem occurred while deserializing the XML data.

LoadXml<T>(string)

Loads an object from an XML file.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
public static T LoadXml<T>(string path)

Parameters

path string

The path of the file to load.

Returns

T

The loaded object.

Type Parameters

T

The type of object the XML stream shall be converted into.

Remarks

Uses AtomicRead internally.

Exceptions

IOException

A problem occurred while reading the file.

UnauthorizedAccessException

Read access to the file is not permitted.

InvalidDataException

A problem occurred while deserializing the XML data.

SaveXml<T>(T, Stream, string?)

Saves an object in an XML stream ending with a line break.

public static void SaveXml<T>(this T data, Stream stream, string? stylesheet = null) where T : notnull

Parameters

data T

The object to be stored.

stream Stream

The stream to write the encoded XML data to.

stylesheet string

The path of an XSL stylesheet; can be null.

Type Parameters

T

SaveXml<T>(T, string, string?)

Saves an object in an XML file ending with a line break.

public static void SaveXml<T>(this T data, string path, string? stylesheet = null) where T : notnull

Parameters

data T

The object to be stored.

path string

The path of the file to write.

stylesheet string

The path of an XSL stylesheet for T; can be null.

Type Parameters

T

The type of object to be saved in an XML stream.

Remarks

This method performs an atomic write operation when possible.

Exceptions

IOException

A problem occurred while writing the file.

UnauthorizedAccessException

Write access to the file is not permitted.

ToXmlString<T>(T, string?)

Returns an object as an XML string ending with a line break.

public static string ToXmlString<T>(this T data, string? stylesheet = null) where T : notnull

Parameters

data T

The object to be stored.

stylesheet string

The path of an XSL stylesheet; can be null.

Returns

string

A string containing the XML code.

Type Parameters

T