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
srcXElementThe source XML element containing the serialized dictionary
baseDirectorystringThe base directory for resolving paths
genModeGenModeThe generation mode to use for deserialization
Returns
- Dictionary<string, T>
A dictionary containing the deserialized objects
Type Parameters
TThe 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
srcXElementThe source XML element that may be a file reference
baseDirectorystringThe base directory for resolving paths
relFilestringOutput 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, bool)
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, bool exhibitionOnly)
Parameters
srcXElementThe source XML element to wrap
baseDirectorystringThe base directory for resolving paths
sourceFilestringThe relative file path to save the element to, or null to return the element as-is
exhibitionOnlyboolSee MakeXmlSource(string, string, bool) for parameter description.
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
srcXElementThe 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
srcINameNoteThe 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
xpathexist, return value byxpath; Otherwise, returndefaultValue.
Type Parameters
Ttype of defaultValue
MakeXmlSourceFile(IMakeXmlSource, string, bool)
Creates an XML source file from the provided source at the specified file path.
public static void MakeXmlSourceFile(this IMakeXmlSource src, string filePath, bool exhibitionOnly = false)
Parameters
srcIMakeXmlSourceThe source that implements IMakeXmlSource interface.
filePathstringThe path where the XML file will be created.
exhibitionOnlyboolSee MakeXmlSource(string, string, bool) for parameter description.
MakeXmlSourceWithRebaseFile(IMakeXmlSource, string, string, bool)
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, bool exhibitionOnly)
Parameters
srcIMakeXmlSourceThe source object to serialize
baseDirectorystringThe base directory for resolving paths
relFilestringThe relative file path to save the XML to, or null to not save to a file
exhibitionOnlyboolSee MakeXmlSource(string, string, bool) for parameter description.
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, bool)
Creates an XML representation of a dictionary of XML-serializable objects.
public static XElement MakeXmlSource<T>(this IDictionary<string, T> dictionary, string baseDirectory, bool exhibitionOnly) where T : IMakeXmlSource
Parameters
dictionaryIDictionary<string, T>The dictionary to serialize
baseDirectorystringThe base directory for resolving paths
exhibitionOnlyboolSee MakeXmlSource(string, string, bool) for parameter description.
Returns
- XElement
An XML element containing the serialized dictionary
Type Parameters
TThe 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
srcIMakeXmlSourceThe XML source to save
baseDirectorystringThe 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
dstINameNoteThe destination INameNote object to update
srcXElementThe 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
srcXElementThe source XElement
xpathstringThe XPath to set the value at
valueTThe value to set
commentstringOptional comment to add above the element
Type Parameters
TThe 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
srcXElementThe source XML element that may contain a file reference
baseDirectorystringThe base directory for resolving paths
setFileActionSetFileDelegateAction 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