Table of Contents

Class XmlUtil

Namespace
Hi.Common.XmlUtils
Assembly
HiGeom.dll

Utility for managing XML.

public static class XmlUtil
Inheritance
XmlUtil
Inherited Members

Fields

FileXName

Alias for XmlFileXName, used for backward compatibility.

public const string FileXName = "XmlSource"

Field Value

string

SelfHostName

Constant representing self-hosted XML content. Used for legacy compatibility.

public const string SelfHostName = "SelfHost"

Field Value

string

XmlFileXName

The XML element name used for file references.

public const string XmlFileXName = "XmlSource"

Field Value

string

Methods

GetDictionaryByXmlSource<T>(XElement, string, GenMode)

Deserializes a dictionary of objects from an XML element.

public static Dictionary<string, T> GetDictionaryByXmlSource<T>(this XElement src, string baseDirectory, GenMode genMode) where T : class

Parameters

src XElement

The source XML element containing the serialized dictionary

baseDirectory string

The base directory for resolving paths

genMode GenMode

The generation mode to use for deserialization

Returns

Dictionary<string, T>

A dictionary containing the deserialized objects

Type Parameters

T

The type of objects to deserialize into the dictionary

GetFileUnwrappedXElement(XElement, string, out string)

Unwraps a file reference XML element to load its actual content.

public static XElement GetFileUnwrappedXElement(this XElement src, string baseDirectory, out string relFile)

Parameters

src XElement

The source XML element that may be a file reference

baseDirectory string

The base directory for resolving paths

relFile string

Output parameter that receives the relative file path if src is a file reference, or null otherwise

Returns

XElement

If src is a file reference element, returns the XML content loaded from the referenced file; otherwise, returns the original element

GetFileWrappedXElement(XElement, string, string)

Wraps an XML element in a file reference element or returns it as-is.

public static XElement GetFileWrappedXElement(this XElement src, string baseDirectory, string sourceFile)

Parameters

src XElement

The source XML element to wrap

baseDirectory string

The base directory for resolving paths

sourceFile string

The relative file path to save the element to, or null to return the element as-is

Returns

XElement

If sourceFile is null, returns the original element; otherwise, saves the element to the specified file and returns a file reference element

GetFirstChildElement(XElement)

Gets the first child element of the source XElement.

public static XElement GetFirstChildElement(this XElement src)

Parameters

src XElement

The source XElement

Returns

XElement

The first child element, or null if no children exist

GetNameNoteXElementList(INameNote)

Creates a list of XML elements representing the name and note properties of an INameNote object.

public static List<XElement> GetNameNoteXElementList(this INameNote src)

Parameters

src INameNote

The source INameNote object

Returns

List<XElement>

A list of XML elements containing the name and note

GetOrDefault<T>(XElement, string, T)

If xpath exist, return value by xpath ; Otherwise, return defaultValue.

The xpath must indicates solely one XElement.

public static T GetOrDefault<T>(this XElement src, string xpath, T defaultValue) where T : IConvertible

Parameters

src XElement

local root element

xpath string

xpath

defaultValue T

default value

Returns

T

If xpath exist, return value by xpath ; Otherwise, return defaultValue.

Type Parameters

T

type of defaultValue

MakeXmlSourceFile(IMakeXmlSource, string)

Creates an XML source file from the provided source at the specified file path.

public static void MakeXmlSourceFile(this IMakeXmlSource src, string filePath)

Parameters

src IMakeXmlSource

The source that implements IMakeXmlSource interface.

filePath string

The path where the XML file will be created.

MakeXmlSourceWithRebaseFile(IMakeXmlSource, string, string)

Creates an XML representation of an object and optionally saves it to a file with path rebasing.

public static XElement MakeXmlSourceWithRebaseFile(this IMakeXmlSource src, string baseDirectory, string relFile)

Parameters

src IMakeXmlSource

The source object to serialize

baseDirectory string

The base directory for resolving paths

relFile string

The relative file path to save the XML to, or null to not save to a file

Returns

XElement

If relFile is null or empty, returns the XML representation of the object; otherwise, saves the XML to the specified file and returns a file reference element

MakeXmlSource<T>(IDictionary<string, T>, string)

Creates an XML representation of a dictionary of XML-serializable objects.

public static XElement MakeXmlSource<T>(this IDictionary<string, T> dictionary, string baseDirectory) where T : IMakeXmlSource

Parameters

dictionary IDictionary<string, T>

The dictionary to serialize

baseDirectory string

The base directory for resolving paths

Returns

XElement

An XML element containing the serialized dictionary

Type Parameters

T

The type of objects in the dictionary, must implement IMakeXmlSource

SaveToByteArrayAsync(IMakeXmlSource, string)

Asynchronously saves an XML source to a byte array.

public static Task<byte[]> SaveToByteArrayAsync(this IMakeXmlSource src, string baseDirectory)

Parameters

src IMakeXmlSource

The XML source to save

baseDirectory string

The base directory for resolving paths

Returns

Task<byte[]>

A byte array containing the serialized XML data

SetNameNote(INameNote, XElement)

Sets the name and note properties of an INameNote object from an XML element.

public static void SetNameNote(this INameNote dst, XElement src)

Parameters

dst INameNote

The destination INameNote object to update

src XElement

The source XML element containing the name and note values

SetOrGenerate<T>(XElement, string, T, string)

Sets a value at the specified XPath, creating the path if it doesn't exist.

public static void SetOrGenerate<T>(this XElement src, string xpath, T value, string comment = null) where T : IConvertible

Parameters

src XElement

The source XElement

xpath string

The XPath to set the value at

value T

The value to set

comment string

Optional comment to add above the element

Type Parameters

T

The type of value to set

UnWrapIfFileBased(XElement, string, SetFileDelegate)

Unwraps an XML element if it contains a file reference element.

public static XElement UnWrapIfFileBased(this XElement src, string baseDirectory, SetFileDelegate setFileAction)

Parameters

src XElement

The source XML element that may contain a file reference

baseDirectory string

The base directory for resolving paths

setFileAction SetFileDelegate

Action to execute with the file path, or null if no file reference is found

Returns

XElement

If the source element contains a file reference, returns the XML content loaded from the referenced file; otherwise, returns the original element