您的位置:首页 > 其它

【转载有关XmlAttribute的知识】

2015-05-08 18:17 288 查看
XmlAttribute类用于描述XML 数据中的元素属性。因为属性的编辑和设置一般使用XmlElement类提供的方法来实现,所以,XmlAttribute类设计的主要目的是为了描述元素属性这样一种XML数据结构。

XMLAttribute类和其他的XmlNode派生类相比,具有一些特殊的地方:该类虽然也是继承于XmlNode ,但是由于该类从属于XmlElement类,不是单独存在的节点——这是DOM标准接口所规定的——所以实际上XmlAttribute不具有子节点。在XmlNode类中定义的一系列子节点操作方法,在这里已经失去了作用。

语法定义:

public class XmlAttribute: XmlNode

因为XmlAttribute类的构造函数是受保护的,所以不能使用其创建XmlAttribute类的实例。一般采用的做法是在XmlDocument类实例中,使用CreateAttribute方法来创建XmlAttribute类的实例。

[html] view plaincopyprint?

XmlDocument doc = new XmlDocument();

//使用属性的限定名创建XmlAttribute类实例

XmlAttribute attr = doc.CreateAttribute("newattribute");

//使用属性的限定名和namespaceURI创建XmlAttribute类实例

XmlAttribute attr = doc.CreateAttribute("xmlns:newattribute","http://www.w3.org/2000/xmlns");

XmlDocument doc = new XmlDocument();
//使用属性的限定名创建XmlAttribute类实例
XmlAttribute attr = doc.CreateAttribute("newattribute");
//使用属性的限定名和namespaceURI创建XmlAttribute类实例
XmlAttribute attr = doc.CreateAttribute("xmlns:newattribute","http://www.w3.org/2000/xmlns");

方法详解

XmlAttribute类虽然包含了XmlNode中的子节点方法,但在XML数据结构中的元素属性不具备子节点,所以实际中并不使用这些方法。大部分操作XmlAttribute类的方法在该类所属的XmlElement中实现了。对于XmlAttribute类本身,主要的方法如下:

Clone:创建当前属性的一个副本

CloneNode:创建当前属性的一个副本,因为XmlAttribute类实际上没有子节点,所以该方法等同于Clone方法

WriteContentTo:使用指定的XmlWriter类实例保存当前元素的所有子节点

WriteTo:使用指定的XmlWrite保存当前元素

[csharp] view plaincopyprint?

static void Main(string[] args)

{

XmlDocument doc = new XmlDocument();

doc.LoadXml("<Data></Data>");

XmlAttribute attr = doc.CreateAttribute("type"); //创建Data元素的属性

attr.Value ="array";

doc.DocumentElement.SetAttributeNode(attr); //添加Data元素属性

//克隆属性,在XmlAttribute类中,CloneNode(true)等同于CloneNode(false),也等同于Clone()

XmlAttribute newattr = (XmlAttribute)attr.CloneNode(true);

newattr.Value = "enum";

doc.DocumentElement.SetAttributeNode(newattr); // 为元素添加同名属性,即修改属性值

Console.WriteLine(doc.OuterXml);

Console.ReadLine();

}

static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Data></Data>");
XmlAttribute attr = doc.CreateAttribute("type");  //创建Data元素的属性
attr.Value ="array";
doc.DocumentElement.SetAttributeNode(attr); //添加Data元素属性
//克隆属性,在XmlAttribute类中,CloneNode(true)等同于CloneNode(false),也等同于Clone()
XmlAttribute newattr = (XmlAttribute)attr.CloneNode(true);
newattr.Value = "enum";
doc.DocumentElement.SetAttributeNode(newattr); // 为元素添加同名属性,即修改属性值
Console.WriteLine(doc.OuterXml);
Console.ReadLine();
}

上面的代码输出结果为:

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

属性详解:

BaseURI:获取属性的基统一资源标识符(URI)

InnerText:获取或指定属性的串联值

InnerXml:获取或指定属性的值

IsReadOnly:获取指示属性是否是只读的值

LocalName:获取属性的本地名称

Name:获取属性的限定名

NamespaceURI:获取属性的命名空间URI

NodeType:获取当前节点的类型

OuterXml:获取表示此节点及其所有子节点的标记

OwnerDocument:获取该节点所属的Xmldocument

OwnerElement:获取该属性的XmlElement

ParentNode:获取该节点的父级,对于XmlAttribute节点,该属性总是返回空引用

Prefix:获取或指定属性的命名空间前缀

Specified:获取一个值,该值指示是否显式指定了属性值

Value:获取或指定属性的值

下面演示如何使用XmlAttribute类的这些属性,代码如下:

[csharp] view plaincopyprint?

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]; //获取XML数据中的元素属性

Console.WriteLine("前缀名:{0}",attr.Prefix); //显示属性的前缀名

Console.WriteLine(“本地名:{0}",attr.LocalName);

Console.WriteLine(“限定名:{0}",attr.Name);

Console.WriteLine(“基URI:{0}",attr.BaseURI);

Console.WriteLine(“属性值:{0}",attr.InnerText);

Console.ReadLine();

}

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];  //获取XML数据中的元素属性
Console.WriteLine("前缀名:{0}",attr.Prefix);  //显示属性的前缀名
Console.WriteLine(“本地名:{0}",attr.LocalName);
Console.WriteLine(“限定名:{0}",attr.Name);
Console.WriteLine(“基URI:{0}",attr.BaseURI);
Console.WriteLine(“属性值:{0}",attr.InnerText);
Console.ReadLine();

}

上面的代码输出结果为:

前缀名:xmlns

本地名:bk

限定名:xmlns:bk

基URI:

属性值:urn:smaples
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: