Class XmlUtil
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
SelfHostName
Constant representing self-hosted XML content. Used for legacy compatibility.
public const string SelfHostName = "SelfHost"
Field Value
XmlFileXName
The XML element name used for file references.
public const string XmlFileXName = "XmlSource"
Field Value
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
XElementThe source XML element containing the serialized dictionary
baseDirectory
stringThe base directory for resolving paths
genMode
GenModeThe 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
XElementThe source XML element that may be a file reference
baseDirectory
stringThe base directory for resolving paths
relFile
stringOutput 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
XElementThe source XML element to wrap
baseDirectory
stringThe base directory for resolving paths
sourceFile
stringThe 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
XElementThe 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
INameNoteThe source INameNote object
Returns
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
Returns
- T
If
xpath
exist, return value byxpath
; Otherwise, returndefaultValue
.
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
IMakeXmlSourceThe source that implements IMakeXmlSource interface.
filePath
stringThe 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
IMakeXmlSourceThe source object to serialize
baseDirectory
stringThe base directory for resolving paths
relFile
stringThe 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
stringThe 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
IMakeXmlSourceThe XML source to save
baseDirectory
stringThe base directory for resolving paths
Returns
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
INameNoteThe destination INameNote object to update
src
XElementThe 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
XElementThe source XElement
xpath
stringThe XPath to set the value at
value
TThe value to set
comment
stringOptional 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
XElementThe source XML element that may contain a file reference
baseDirectory
stringThe base directory for resolving paths
setFileAction
SetFileDelegateAction 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