"Reprint of Knowledge about XmlAttribute"

Source: Internet
Author: User
Tags baseuri

The XmlAttribute class is used to describe element attributes in XML data. Because the editing and setting of attributes is generally implemented using the methods provided by the XmlElement class, the main purpose of the XmlAttribute class design is to describe an XML data structure such as an element property.

Compared to other XmlNode derived classes, the XmlAttribute class has some special places: Although the class is also inherited from XmlNode, the class is subordinate to the XmlElement class, not a separate node--as specified by the DOM standard interface-- So actually XmlAttribute does not have child nodes. A series of sub-node operation methods defined in the XmlNode class have lost their role here.

Syntax Definitions:

public class Xmlattribute:xmlnode

Because the constructor of the XmlAttribute class is protected, you cannot use it to create an instance of the XmlAttribute class. The general practice is to use the CreateAttribute method in the XmlDocument class instance to create an instance of the XmlAttribute class.

[HTML]View Plaincopyprint?
    1. XmlDocument doc = new XmlDocument ();
    2. Create an instance of the XmlAttribute class using the qualified name of the attribute
    3. XmlAttribute attr = doc.  CreateAttribute ("Newattribute");
    4. Create a XmlAttribute class instance using the qualified name and NamespaceURI of the attribute
    5. XmlAttribute attr = doc.  CreateAttribute ("Xmlns:newattribute", "Http://www.w3.org/2000/xmlns");
XmlDocument doc = new XmlDocument ();//Use the qualified name of the attribute to create the XmlAttribute class instance XmlAttribute attr = Doc. CreateAttribute ("Newattribute");//Use the qualified name of the attribute and NamespaceURI to create the XmlAttribute class instance XmlAttribute attr = Doc. CreateAttribute ("Xmlns:newattribute", "Http://www.w3.org/2000/xmlns");

Detailed method

Although the XmlAttribute class contains child node methods in XmlNode, the element attributes in the XML data structure do not have child nodes, so these methods are not used in practice. Most methods of manipulating the XmlAttribute class are implemented in the XmlElement that the class belongs to. For the XmlAttribute class itself, the main method is as follows:

Clone: Create a copy of the current property

CloneNode: Creates a copy of the current property because the XmlAttribute class does not actually have child nodes, so the method is equivalent to the Clone method

WriteContentTo: Saves all child nodes of the current element using the specified XmlWriter class instance

WriteTo: Saves the current element with the specified xmlwrite

[CSharp]View Plaincopyprint?
  1. static void Main (string[] args)
  2. {
  3. XmlDocument doc = new XmlDocument ();
  4. Doc.  LOADXML ("<Data></Data>");
  5. XmlAttribute attr = doc.  CreateAttribute ("type"); //Create a property of the data element
  6. attr.  Value ="Array";
  7. Doc. Documentelement.setattributenode (attr); //Add data element properties
  8. //Clone attribute, in the XmlAttribute class, CloneNode (True) is equivalent to CloneNode (false) and is also equivalent to clone ()
  9. XmlAttribute newattr = (XmlAttribute) attr.  CloneNode (true);
  10. Newattr.  Value = "enum";
  11. Doc. Documentelement.setattributenode (NEWATTR); //Add the same name attribute to the element, i.e. modify the property value
  12. Console.WriteLine (Doc. OuterXml);
  13. Console.ReadLine ();
  14. }
static void Main (string[] args) {    XmlDocument doc = new XmlDocument ();    Doc. LOADXML ("<Data></Data>");    XmlAttribute attr = doc. CreateAttribute ("type");  Creates a property attr for the data element    . Value = "Array";    Doc. Documentelement.setattributenode (attr); Add the Data Element property    //Clone property, in the XmlAttribute class, CloneNode (True) is equivalent to CloneNode (false), or clone ()    XmlAttribute newattr = (XmlAttribute) attr. CloneNode (true);    Newattr. Value = "enum";    Doc. Documentelement.setattributenode (NEWATTR); Adds a property of the same name to the element, which modifies the property value     Console.WriteLine (Doc. OuterXml);    

The above code outputs the result:

<data type= "enum" ></Data>

Attribute Details:

BaseURI: Gets the base Uniform Resource Identifier (URI) of the property

InnerText: Gets or specifies the concatenated value of the property

INNERXML: Gets or specifies the value of the property

IsReadOnly: Gets a value that indicates whether the property is read-only

LocalName: Gets the local name of the property

Name: Gets the qualified name of the property

NamespaceURI: Gets the namespace URI of the property

NodeType: Gets the type of the current node

OuterXml: Gets the token that represents this node and all its child nodes

Ownerdocument: Gets the XmlDocument that the node belongs to

Ownerelement: Gets the XmlElement of the property

ParentNode: Gets the parent of the node, which always returns a null reference for the XmlAttribute node

Prefix: Gets or specifies the namespace prefix of the property

Specified: Gets a value that indicates whether the property value is explicitly specified

Value: Gets or specifies the value of the property

The following shows how to use these properties of the XmlAttribute class with the following code:

[CSharp]View Plaincopyprint?
  1. static void Main (string[] args)
  2. {
  3. XmlDocument doc = new XmlDocument ();
  4. Docloadxml ("<book xmls:bk= ' urn:samples ' bk:genre= ' novel ' >" + "<title>price and prejudice</  Title></book> ");
  5. XmlAttribute attr = doc.  DOCUMENTELEMENT.ATTRIBUTE[0]; //Get element attributes in XML data
  6. Console.WriteLine ("prefix name: {0}", attr.  Prefix); //Display the prefix name of the property
  7. Console.WriteLine ("Local name: {0}", attr. LocalName);
  8. Console.WriteLine ("Qualified name: {0}", attr. Name);
  9. Console.WriteLine ("Base uri:{0}", attr. BaseURI);
  10. Console.WriteLine ("Property value: {0}", attr. InnerText);
  11. Console.ReadLine ();
  12. }
static void Main (string[] args) {   XmlDocument doc = new XmlDocument ();   docloadxml ("<book xmls:bk=" urn: Samples ' bk:genre= ' novel ' > "+" <title>price and prejudice</title></book> ");   XmlAttribute attr = doc. DOCUMENTELEMENT.ATTRIBUTE[0];  Gets the element attribute in the XML data   Console.WriteLine ("prefix name: {0}", attr. Prefix);  The prefix name of the display property is   Console.WriteLine ("Local name: {0}", attr. LocalName);    Console.WriteLine ("Qualified name: {0}", attr. Name);    Console.WriteLine ("Base uri:{0}", attr. BaseURI);    Console.WriteLine ("Property value: {0}", attr. InnerText);    Console.ReadLine ();}

The above code outputs the result:

Prefix Name: xmlns

Local name: BK

Qualified Name: XMLNS:BK

Base URI:

Property value: Urn:smaples

"Reprint of Knowledge about XmlAttribute"

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.