Chilkat C# Xml Class Reference

Xml

A free non-validating XML parser component with encryption and compression features.

Object Creation

(C#)
Chilkat.Xml obj = new Chilkat.Xml();
(VB.NET)
Dim obj As New Chilkat.Xml()

Properties

public bool Cdata {get; set; }

When True, causes an XML node's content to be encapsulated in a CDATA section.

public string Content {get; set; }

This is the content of the XML node. It is the text between the open and close tags, such as:This is the content

Content vs. Children: A common misconception explained.

public int ContentInt {get; set; }

Set/get the content as an integer.

public string Encoding {get; set; }

This is the encoding attribute in the XML declaration, such as "utf-8" or "iso-8859-1". If not present, this property returns an empty string. This property can be set from any node in the XML document and when set, causes the encoding property to be added to the XML declaration. Setting this property does not cause the document to be converted to a different encoding.

public string LastErrorHtml {get; }

To be documented soon.Error information in HTML format for the last method called.

public string LastErrorText {get; }

To be documented soon.Error information in plain-text format for the last method called.

public string LastErrorXml {get; }

To be documented soon.Error information in XML format for the last method called.

public int NumAttributes {get; }

The number of attributes. For example, the following node has 2 attributes: This is the content

public int NumChildren {get; }

The number of child nodes contained under this XML node.

public bool SortCaseInsensitive {get; set; }

If true (or 1 for ActiveX), then all Sort* methods use case insensitive sorting.

public bool Standalone {get; set; }

This is the standalone attribute in the XML declaration. This property can be set from any node in the XML document. A value of true adds a standalone="yes" to the XML declaration:

<?xml ... standalone="yes">

public string Tag {get; set; }

The XML node's tag.

public int TreeId {get; }

Each tree (or XML document) has a unique TreeId. This is the ID of the tree, and can be used to determine if two Xml objects belong to the same tree.

public string Version {get; }

The version of the XML component, such as "2.0.0".

Methods

public string AccumulateTagContent(string tag, string skipTags);

Accumulates the content of all nodes having a specific tag into a single result string. SkipTags specifies a set of tags whose are to be avoided. The skipTags are formatted as a string of tags delimited by vertical bar characters. All nodes in sub-trees rooted with a tag appearing in skipTags are not included in the result.

Returns a null on failure

public bool AddAttribute(string name, string value);

Adds an attribute to the calling node in the XML document. Returns True for success, and False for failure.

public bool AddAttributeInt(string name, int value);

Adds an integer attribute to a node.

public bool AddChildTree(Xml tree);

Adds an entire subtree as a child. If the child was a subtree within another Xml document then the subtree is effectively transferred from one XML document to another.

public void AddOrUpdateAttribute(string name, string value);

Adds an attribute to an XML node. If an attribute having the specified name already exists, the value is updated.

public void AddOrUpdateAttributeI(string name, int value);

Adds an integer attribute to an XML node. If an attribute having the specified name already exists, the value is updated.

public void AddStyleSheet(string styleSheet);

Adds a style sheet declaration to the XML document. The styleSheet should be a string such as:

<?xml-stylesheet href="mystyle.css" title="Compact" type="text/css"?>

public void AddToAttribute(string name, int amount);

Adds an integer amount to an integer attribute's value. If the attribute does not yet exist, this method behaves the same as AddOrUpdateAttributeI.

public void AddToChildContent(string tag, int amount);

Adds an integer value to the content of a child node.

public void AddToContent(int amount);

Adds an integer amount to the node's content.

public bool AppendToContent(string str);

Appends text to the content of an XML node

public bool BEncodeContent(string charset, byte[] inData);

Sets the node's content with 8bit data that is in a specified multibyte character encoding such as utf-8, shift-jis, big5, etc. The data is first B-encoded and the content is set to be the B-encoded string. For example, if called with "Big5"for the charset, you would get a string that looks something like this: "=?Big5?B?pHCtsw==?=". The data is Base64-encoded and stored between the last pair of "?" delimiters. Use the DecodeContent method to retrieve the byte data from a B encoded string.

public bool ChildContentMatches(string tag, string pattern, bool caseSensitive);

Return true if a child having a specific tag contains content that matches a wildcarded pattern.

public void Clear();

Removes all children, attributes, and content from the XML node. Resets the tag name to "unnamed".

public bool ContentMatches(string pattern, bool caseSensitive);

Return true if the node's content matches a wildcarded pattern.

public void Copy(Xml node);

Copies the tag, content, and attributes to the calling node.

public byte[] DecodeContent();

Decodes a node's Q or B-encoded content string and returns the byte data.

Returns null on failure

public string DecodeEntities(string str);

Utility method to decode HTML entities. It accepts a string containing (potentially) HTML entities and returns a string with the entities decoded.

Returns a null on failure

public bool DecryptContent(string password);

Decrypts the content of an XML node that was previously 128-bit AES encrypted with the EncryptContent method.

public bool EncryptContent(string password);

AES encrypts the content of the calling XML node using 128-bit Rijndael encryption.

public Xml ExtractChildByIndex(int index);

Removes and returns the Nth child of an XML node. The first child is at index 0.

public Xml ExtractChildByName(string tag, string attrName, string attrValue);

Removes and returns the first child node having a tag equal to the tagName. The attributeName and attrValue may be empty or NULL, in which case the first child matching the tag is removed and returned. If attributeName is specified, then the first child having a tag equal to tagName, and an attribute with attributeName is returned. If attrValue is also specified, then only a child having a tag equal to tagName, and an attribute named attributeName, with a value equal to attrValue is returned.

public Xml FindChild(string tag);

Returns the child having a specified tag.

public bool FindChild2(string tag);

Updates the Xml object's internal reference to point to a child with a specified tag.

public Xml FindNextRecord(string tag, string contentPattern);

Returns the next record node where the child with a specific tag matches a wildcarded pattern. This method makes it easy to iterate over high-level records.

public Xml FindOrAddNewChild(string tag);

First searches for a child having a tag equal to tagName, and if found, returns it. Otherwise creates a new child, sets the tag equal to tagName, and initializes the Content to empty.

public Xml FirstChild();

Returns the first child. A program can step through the children by calling FirstChild, and then NextSibling repeatedly.

public bool FirstChild2();

Updates the internal reference of the caller to point to its first child.

More information about XML methods ending in "2"

public string GetAttrValue(string name);

Find and return the value of an attribute having a specified name.

Returns a null on failure

public int GetAttrValueInt(string name);

Returns an attribute as an integer.

public string GetAttributeName(int index);

Returns the name of the Nth attribute of an XML node. The first attribute is at index 0.

Returns a null on failure

public string GetAttributeValue(int index);

Returns the value of the Nth attribute of an XML node. The first attribute is at index 0.

Returns a null on failure

public int GetAttributeValueInt(int index);

Returns an attribute as an integer.

public byte[] GetBinaryContent(bool unzipFlag, bool decryptFlag, string password);

Returns binary content of an XML node as a byte array. The content may have been Zip compressed, AES encrypted, or both. Unzip compression and AES decryption flags should be set appropriately.

Returns null on failure

public Xml GetChild(int index);

Returns the Nth child of an XML node

public bool GetChild2(int index);

Updates the calling object's internal reference to the Nth child node.

Returns true for success, false for failure.

More information about XML methods ending in "2"

public bool GetChildBoolValue(string tag);

Returns false if the node's content is "0", otherwise returns true if the node contains a non-zero integer.

public string GetChildContent(string tag);

Returns the content of a child having a specified tag.

Returns a null on failure

public string GetChildContentByIndex(int index);

Returns the content of the Nth child node.

Returns a null on failure

public Xml GetChildExact(string tag, string content);

Returns the child having the exact tag and content.

public int GetChildIntValue(string tag);

Returns the child integer content for a given tag.

public string GetChildTag(int index);

Returns the tag name of the Nth child node.

Returns a null on failure

public string GetChildTagByIndex(int index);

Returns the tag name of the Nth child node.

Returns a null on failure

public Xml GetChildWithTag(string tag);

Returns the Xml child object having a tag matching tagName.

public Xml GetNthChildWithTag(string tag, int n);

Returns the Nth child having a tag that matches exactly with the tagName. Use the NumChildrenHavingTag method to determine how many children have a particular tag.

public bool GetNthChildWithTag2(string tag, int n);

Updates the calling object's internal reference to the Nth child node having a specific tag.

Returns true for success, false for failure.

public Xml GetParent();

Returns the parent of this XML node, or NULL if the node is the root of the tree.

public bool GetParent2();

Updates the internal reference of the caller to its parent.

More information about XML methods ending in "2"

public Xml GetRoot();

Returns the root node of the XML document

public void GetRoot2();

Updates the internal reference of the caller to the document root.

More information about XML methods ending in "2"

public string GetXml();

Generate the XML text document for the XML tree rooted at this node.

Returns a null on failure

public bool HasAttrWithValue(string name, string value);

Returns true if the node contains attribute with the name and value.

public bool HasAttribute(string name);

Returns true if the node contains an attribute with the specified name.

public bool HasChildWithContent(string content);

Returns true if the node has a child node containing the exact content string specified.

public bool HasChildWithTag(string tag);

Returns true (1 for ActiveX) if the node has a child with a given tag.

public bool HasChildWithTagAndContent(string tag, string content);

Returns true if the node contains a child having the exact tag and content specified.

public Xml HttpGet(string url);

Gets an XML document from a URL. This can be tested with the following URL: http://www.chilkatsoft.com/test.xml

public Xml LastChild();

Returns the last Xml child node. A node's children can be enumerated by calling LastChild and then repeatedly calling PreviousSibling, until a NULL is returned.

public bool LastChild2();

Updates the internal reference of the caller to its last child.

public bool LoadXml(string xmlData);

Loads an XML document from a memory buffer and returns True if successful. The contents of the calling node are replaced with the root node of the XML document loaded.

public bool LoadXml2(string xmlData, bool autoTrim);

Same as LoadXml, but an additional argument controls whether or not leading/trailing whitespace is auto-trimmed from each node's content.

Returns true for success, false for failure.

public bool LoadXmlFile(string fileName);

Loads an XML document from a file and returns True if successful. The contents of the calling node are replaced with the root node of the XML document loaded.

public bool LoadXmlFile2(string fileName, bool autoTrim);

Same as LoadXmlFile, but an additional argument controls whether or not leading/trailing whitespace is auto-trimmed from each node's content.

Returns true for success, false for failure.

public Xml NewChild(string tag, string content);

Creates a new child having tag and content. The new child is created even if a child with a tag equal to tagName already exists. (Use FindOrAddNewChild to prevent creating children having the same tags.)

public void NewChild2(string tag, string content);

Creates a new child but does not return the node that is created.

public void NewChildInt2(string tag, int value);

Inserts a new child having an integer for content.

public Xml NextSibling();

Returns the nodes next sibling, or NULL if there are no more.

public bool NextSibling2();

Updates the internal reference of the caller to its next sibling.

More information about XML methods ending in "2"

public int NumChildrenHavingTag(string tag);

Returns the number of children having a specific tag name.

public Xml PreviousSibling();

Returns the Xml object that is the node's previous sibling, or NULL if there are no more.

public bool PreviousSibling2();

Updates the internal reference of the caller to its previous sibling.

More information about XML methods ending in "2"

public bool QEncodeContent(string charset, byte[] inData);

Sets the node's content with 8bit data that is in a specified multibyte character encoding such as utf-8, shift-jis, big5, etc. The data is first Q-encoded and the content is set to be the Q-encoded string. For example, if called with "gb2312"for the charset, you would get a string that looks something like this: "=?gb2312?Q?=C5=B5=BB=F9?=". Character that are not 7bit are represented as "=XX" where XX is the hexidecimal value of the byte. Use the DecodeContent method to retrieve the byte data from a Q encoded string.

public bool RemoveAllAttributes();

Removes all attributes from an XML node. Should always return True.

public void RemoveAllChildren();

Removes all children from the calling node.

public bool RemoveAttribute(string name);

Removes an attribute by name from and XML node.

public void RemoveChild(string tag);

Removes a child with a given tag.

public void RemoveChildByIndex(int index);

Removes the Nth child from the calling node.

public void RemoveChildWithContent(string content);

Removes all children having the exact content specified.

public void RemoveFromTree();

Removes the calling object and its sub-tree from the XML document making it the root of its own tree.

public bool SaveBinaryContent(string filename, bool unzipFlag, bool decryptFlag, string password);

Saves a node's binary content to a file.

public bool SaveLastError(string filename);

Saves the last error information to an XML formatted file.

public bool SaveXml(string fileName);

Generates XML representing the tree or subtree rooted at this node and writes it to a file.

public Xml SearchAllForContent(Xml after, string contentPattern);

Returns the first node whose content matches the contentPattern, which is a case-sensitive string that can use any number of '*'s to represent 0 or more occurances of any character. The search is breadth-first over the tree, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

public bool SearchAllForContent2(Xml after, string contentPattern);

Same as SearchAllForContent except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

public Xml SearchForAttribute(Xml after, string tag, string attr, string valuePattern);

Returns the first node having a tag equal to tagName, and an attribute named attrName whose value matches valuePattern, which is a case-sensitive string that can use any number of '*'s to represent 0 or more occurances of any character. The search is breadth-first over the tree, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

public bool SearchForAttribute2(Xml after, string tag, string attr, string valuePattern);

Same as SearchForAttribute except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

public Xml SearchForContent(Xml after, string tag, string contentPattern);

Returns the first node having a tag equal to tagName whose content matches contentPattern, which is a case-sensitive string that can use any number of '*'s to represent 0 or more occurances of any character. The search is breadth-first over the tree, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

public bool SearchForContent2(Xml after, string tag, string contentPattern);

Same as SearchForContent except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

public Xml SearchForTag(Xml after, string tag);

Returns the first node having a tag equal to tagName. The search is breadth-first over the tree, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

public bool SearchForTag2(Xml after, string tag);

Same as SearchForTag except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

public bool SetBinaryContent(byte[] inData, bool zipFlag, bool encryptFlag, string password);

Sets the node's content to a block of binary data with optional Zip compression and/or AES encryption. The binary data is automatically converted to base64 format whenever XML text is generated. If the zipFlag is True, the data is first compressed. If the encryptFlag is True, the data is AES encrypted using the Rijndael 128-bit symmetric-encryption algorithm.

public bool SetBinaryContentFromFile(string filename, bool zipFlag, bool encryptFlag, string password);

Sets the node's content with binary (or text) data from a file. The file contents can be Zip compressed and/or encrypted, and the result is base-64 encoded.

public void SortByAttribute(string attrName, bool ascending);

Sorts the child nodes by the value of a specified attribute.

public void SortByAttributeInt(string attrName, bool ascending);

Sorts the child nodes by the value of a specified attribute interpreted as an integer (not lexicographically as strings).

public void SortByContent(bool ascending);

Sorts the child nodes by content.

public void SortByTag(bool ascending);

Sorts the child nodes by tag.

public void SortRecordsByAttribute(string sortTag, string attrName, bool ascending);

Sorts the child nodes by the content of an attribute in the grandchild nodes.

public void SortRecordsByContent(string sortTag, bool ascending);

Sorts the child nodes by the content of the grandchild nodes.

public bool SwapNode(Xml node);

Swaps another node's tag, content, and attributes with this one.

public bool SwapTree(Xml tree);

Swaps another node's tag, content, attributes, and children with this one.

public bool TagEquals(string tag);

Returns true if the node's tag equals the specified string.

public bool UnzipContent();

Unzip the content of the XML node replacing it's content with the decompressed data.

public bool UnzipTree();

Unzips and recreates the XML node and the entire subtree, restoring it to the state before it was zip compressed.

public bool UpdateAttribute(string attrName, string attrValue);

Adds an attribute to the node if it doesn't already exist. Otherwise it updates the existing attribute with the new value.

public bool UpdateAttributeInt(string attrName, int value);

Updates an attribute value. (Call UpdateAttribute if the attribute value is a string.)

public void UpdateChildContent(string tag, string value);

Replaces the content of a child node.

public void UpdateChildContentInt(string tag, int value);

Replaces the content of a child node where the content is an integer.

public bool ZipContent();

Applies Zip compression to the content of an XML node and replaces the content with base64-encoded compressed data.

public bool ZipTree();

Zip compresses the content and entire subtree rooted at the calling XML node and replaces the current content with base64-encoded Zip compressed data. The node and subtree can be restored by calling UnzipTree. Note that the node name and attributes are unaffected.