Basic XML operations

Source: Internet
Author: User
Tags processing instruction
Xmlnode. Attributes attributes
Description: xmlattributecollection, which contains the attributes of the node. If the node is of the xmlnodetype. Element type, the attribute of the node is returned. Otherwise, this property returns a null reference (nothing in Visual Basic ).

Example: The following example adds a new attribute to the XML document.
Public shared sub main ()
Dim doc as new xmldocument ()
Doc. loadxml ("<book xmlns: BK = 'urn: samples' BK: ISBN = '1-861001-57-5 '> "&_
"<Title> Pride and Prejudice </title> "&_
"</Book> ")
Dim root as xmlnode = Doc. firstchild
'Add a new node attribute
Dim ns as string = root. getnamespaceofprefix ("BK ")
Dim ATTR as xmlnode = Doc. createnode (xmlnodetype. attribute, "genre", NS)
ATTR. value = "novel"
'Add the new property to the document
Root. Attributes. setnameditem (ATTR)
Console. writeline ("display the modified XML ...")
Doc. Save (console. out)
End sub

========================================================== ========

XmlNode. ChildNodes attribute

Description: obtains all subnodes of a node.
Attribute Value: An XmlNodeList that contains all the subnodes of a node. If no subnode exists, this attribute returns an empty XmlNodeList.

Example: The following example shows all the child nodes of the root element.

Public Shared Sub Main ()
Dim doc As New XmlDocument ()
Doc. LoadXml ("<book ISBN = '1-861001-57-5 '> "&_
"<Title> Pride And Prejudice </title> "&_
"<Price> 19.95 </price> "&_
"</Book> ")
Dim root As XmlNode = doc. FirstChild
'Display the contents of the child nodes.
If root. HasChildNodes Then
Dim I As Integer
For I = 0 To root. ChildNodes. Count-1
Console. WriteLine (root. ChildNodes (I). InnerText)
Next I
End If
End Sub

========================================================== ========

Xmlnode. firstchild attribute

Description: gets the first sublevel of a node. If such a node does not exist, a null reference (nothing in Visual Basic) is returned ).

Example: The following example shows the title element.

Public shared sub main ()
Dim doc as new xmldocument ()
Doc. loadxml ("<book ISBN = '1-861001-57-5 '> "&_
"<Title> Pride and Prejudice </title> "&_
"<Price> 19.95 </price> "&_
"</Book> ")
Dim root as xmlnode = Doc. firstchild
Console. writeline ("display the title element ...")
Console. writeline (root. firstchild. outerxml)
End sub 'main

========================================================== ========

Xmlnode. haschildnodes attribute

Description: gets a value indicating whether a node has any subnodes. If a node has a subnode, the value is true; otherwise, the value is false.

Example: The following example shows all the child nodes of the root element.

Public shared sub main ()
Dim doc As New XmlDocument ()
Doc. LoadXml ("<book ISBN = '1-861001-57-5 '> "&_
"<Title> Pride And Prejudice </title> "&_
"<Price> 19.95 </price> "&_
"</Book> ")
Dim root As XmlNode = doc. FirstChild
'Display the contents of the child nodes.
If root. HasChildNodes Then
Dim I As Integer
For I = 0 To root. ChildNodes. Count-1
Console. WriteLine (root. ChildNodes (I). InnerText)
Next I
End If
End Sub 'main

========================================================== ========

XmlNode. InnerText attributes

Description: gets or sets the serial value of a node and all its subnodes.
The serial value of the node and all its subnodes.
Note: setting this attribute will replace all subnodes with the analyzed given string content.
For leaf nodes, the InnerText and Value Attributes return the same content.

Example: The following example compares the InnerText and InnerXml attributes.

Public shared sub Main ()

Dim doc as XmlDocument = new XmlDocument ()
Doc. LoadXml ("<root> "&_
"<Elem> some text <child/> more text </elem> "&_
"</Root> ")

Dim elem as XmlNode = doc. DocumentElement. FirstChild

'Note that InnerText does not include the markup.
Console. WriteLine ("Display the InnerText of the element ...")
Console. WriteLine (elem. InnerText)

'Initxml parameters des the markup of the element.
Console. WriteLine ("Display the InnerXml of the element ...")
Console. WriteLine (elem. InnerXml)

'Set InnerText to a string that includes des markup.
'The markup is escaped.
Elem. InnerText = "Text containing <markup/> will have char (<) and char (>) escaped ."
Console. WriteLine (elem. OuterXml)

'Set innerxml to a string that includes des markup.
'The markup is not escaped.
ELEM. innerxml = "text containing <markup/> ."
Console. writeline (ELEM. outerxml)
End sub

========================================================== ========

Xmlnode. innerxml attributes

Description: gets or sets the flag that only represents the subnode of the node.

Note: setting this attribute from a node (such as a text node) that cannot have a subnode will cause an exception. Setting innerxml will replace the child node of the node with the analyzed given string content. The analysis is completed in the context of the current namespace.

========================================================== ========

Xmlnode. isreadonly attribute

Description: gets a value indicating whether the node is read-only. If the node is read-only, true is used; otherwise, false is used.

Note: a read-only node is a node that cannot be changed for attributes, features, or subnodes. You can remove read-only nodes from the tree and insert them to other locations. For example, the entity node is always read-only.

========================================================== ========

Xmlnode. Item attributes

Description: gets the specified child element.

Reload list:

Description

Xmlnode. Item (string) gets the first child element with the specified name.

Supported by. NET Compact Framework.
XmlNode. Item (String, String) gets the first child element with the specified LocalName and NamespaceURI.
Supported by. NET Compact Framework.

------------------------------------
XmlNode. Item attribute (String)

Description: gets the first child element with the specified Name.

Example: The following example shows the title element.

Public Shared Sub Main ()
Dim doc As New XmlDocument ()
Doc. LoadXml ("<book ISBN = '1-861001-57-5 '> "&_
"<Title> Pride And Prejudice </title> "&_
"<Price> 19.95 </price> "&_
"</Book> ")

Dim root As XmlNode = doc. FirstChild
Console. WriteLine ("Display the title element ...")
Console. WriteLine (root ("title"). OuterXml)
End Sub 'main

------------------------------------

XmlNode. Item attributes (String, String)

Description: gets the first child element with the specified LocalName and NamespaceURI.

Parameters:
The local name of the localname element.
The namespace URI of the ns element.

========================================================== ========

XmlNode. LastChild attributes

Description: gets the last sublevel of a node. If such a node does not exist, a null reference (Nothing in Visual Basic) is returned ).

Example: The following example shows the price element.

Public Shared Sub Main ()
Dim doc As New XmlDocument ()
Doc. LoadXml ("<book ISBN = '1-861001-57-5 '> "&_
"<Title> Pride And Prejudice </title> "&_
"<Price> 19.95 </price> "&_
"</Book> ")
Dim root As XmlNode = doc. FirstChild
Console. WriteLine ("Display the price element ...")
Console. WriteLine (root. LastChild. OuterXml)
End Sub 'main

========================================================== ========

XmlNode. LocalName attribute

Description: gets the local name of a node when it is overwritten in a derived class. If the node has no prefix, The LocalName and Name are the same.

The name of the node whose prefix is removed. For example, for the element <bk: book>, LocalName is book. The returned name depends on the node's NodeType:

========================================================== ========

XmlNode. NodeType attribute

Description: gets the type of the current node when it is overwritten in a derived class. This attribute never returns XmlNodeType EndElement, EndEntity, or None.

========================================================== ========

XmlNode. NextSibling attributes

Description: gets the node that follows the node. Next XmlNode. If there is no next node, a null reference (Nothing in Visual Basic) is returned ).

Example: The following example shows all the books in the XML document.

Public shared sub Main ()

Dim doc as XmlDocument = new XmlDocument ()
Doc. Load ("books. xml ")

Dim currNode as XmlNode = doc. DocumentElement. FirstChild
Console. WriteLine ("First book ...")
Console. WriteLine (currNode. OuterXml)

Dim nextNode as XmlNode = currNode. NextSibling
Console. WriteLine (ControlChars. LF + "Second book ...")
Console. WriteLine (nextNode. OuterXml)

End sub

========================================================== ========

XmlNode. OuterXml attributes

Description: gets the flag indicating this node and all its subnodes. Tags that contain this node and all its subnodes. OuterXml does not return the default attribute.

Example: The following example compares the output from the InnerXml and OuterXml attributes.

Public Shared Sub Main ()
Dim doc As New XmlDocument ()
Doc. LoadXml ("<book genre = 'novel' ISBN = '1-861001-57-5 '> "&_
"<Title> Pride And Prejudice </title> "&_
"</Book> ")
Dim root As XmlNode = doc. DocumentElement
'Outerxml nodes the markup of current node.
Console. WriteLine ("Display the OuterXml property ...")
Console. WriteLine (root. OuterXml)
'Initxml does not include the markup of the current node.
As a result, the attributes are not displayed.
Console. WriteLine ()
Console. WriteLine ("Display the InnerXml property ...")
Console. WriteLine (root. InnerXml)
End Sub 'main

========================================================== ========

XmlNode. OwnerDocument attributes

Description: gets the XmlDocument to which the node belongs. If the node is an XmlDocument (NodeType equals XmlNodeType. Document), this attribute returns a null reference (Nothing in Visual Basic ). When adding a node to the current node, use the XmlDocument returned by the OwnerDocument attribute to create the node.

Example: For an example of using this property, see OwnerDocument (in the XmlElement class ).

========================================================== ========

XmlNode. ParentNode attributes

Description: gets the parent level of a node (for a node with a parent level. It is the XmlNode at the parent level of the current node. If the node has just been created and has not been added to the tree, or if it has been removed from the tree, the parent level is a null reference (Nothing in Visual Basic ). For all other nodes, the returned value depends on the node's NodeType. The following table describes the possible return values of the ParentNode attribute.

Nodetype ----------------- return value of parentnode
Attribute, document
Documentfragment returns an empty reference (nothing in Visual Basic); these nodes do not have a parent level.
Entity, notation
CDATA returns the element or entity reference that contains the CDATA section.
Comment returns the elements, entity references, document types, or documents that contain comments.
Documenttype returns the document node.
Element returns the parent node of the element. If this element is the root node in the tree, the parent level is the document node.
Entityreference returns the element, attribute, or object reference that contains the object reference.
Processinginstruction returns the document, element, document type, or entity reference containing the processing instruction.
Text returns the parent element, attribute, or object reference that contains the text node.
========================================================== ========

Xmlnode. prefix attribute

Description: gets or sets the namespace prefix of the node. For example, for the element <BK: Book>, the prefix is BK. If there is no prefix, the property returns string. Empty.

If this Attribute is enabled, the Name Attribute on the Element and Attribute nodes is changed. The latter has a qualified Name. This attribute is set to invalid for node types that cannot have a prefix (such as Text, Comment, EntityReference, CDATA, ProcessingInstruction, Document, and DocumentFragment. Changing the prefix of an attribute with the default value is not allowed to generate a new attribute with the default value. The original prefix is displayed because the URI and local name of the namespace are not changed.

========================================================== ========

XmlNode. previussibling attributes

Description: gets the node that follows the node. The previous XmlNode. If no node exists, a null reference (Nothing in Visual Basic) is returned ).

Example: The following example shows all the books in the XML document.

Public shared sub Main ()

Dim doc as XmlDocument = new XmlDocument ()
Doc. Load ("books. xml ")

Dim lastNode as XmlNode = doc. DocumentElement. LastChild
Console. WriteLine ("Last book ...")
Console. WriteLine (lastNode. OuterXml)

Dim prevNode as XmlNode = lastNode. previussibling
Console. WriteLine (ControlChars. LF + "Previous book ...")
Console. WriteLine (prevNode. OuterXml)
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.