LINQ To XML:获取,修改,增加,删除元素的属性
2011-04-25 16:26
429 查看
获取元素属性有两个方法,XElement.Attribute() 和XElement.Attributes()
简单举个例就知道了
输出
type="Author"
experience="first-time"
那么往元素上加属性有如下方法
XElement.Add()
XElement.AddFirst()
XElement.AddBeforeThis()
XElement.AddAfterThis()
删除元素属性有如下方法
XAttribute.Remove()
更新元素属性是这样的
XElement.Attribute("someattribute").Value = "beginner"
同样的属性也有XElement.SetAttributeValue() 方法
用起来和XElement.SetElementValue是一样的,这里不举例了
简单举个例就知道了
// we will use this to store a reference to one of the elements in the XML tree. XElement firstParticipant; XDocument xDocument = new XDocument( new XElement("BookParticipants", firstParticipant = new XElement("BookParticipant", new XAttribute("type", "Author"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")))); Console.WriteLine(firstParticipant.Attribute("type").Value);
// we will use this to store a reference to one of the elements in the XML tree. XElement firstParticipant; XDocument xDocument = new XDocument( new XElement("BookParticipants", firstParticipant = new XElement("BookParticipant", new XAttribute("type", "Author"), new XAttribute("experience", "first-time"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")))); foreach(XAttribute attr in firstParticipant.Attributes()) { Console.WriteLine(attr); }
输出
type="Author"
experience="first-time"
那么往元素上加属性有如下方法
XElement.Add()
XElement.AddFirst()
XElement.AddBeforeThis()
XElement.AddAfterThis()
删除元素属性有如下方法
XAttribute.Remove()
更新元素属性是这样的
XElement.Attribute("someattribute").Value = "beginner"
同样的属性也有XElement.SetAttributeValue() 方法
用起来和XElement.SetElementValue是一样的,这里不举例了
相关文章推荐
- C#使用linq to xml完成对XML文件的创建、增加、删除、修改、查询
- LinqToSql增加、修改、删除---(转载)
- js+jquery动态设置/增加/删除/获取元素属性的两种方法集锦对比(动态onclick属性设置+动态title设置)
- dom4j修改,获取,增加xml中某个元素的属性值
- c# linq to xml 获取某节点属性总数
- c#对xml多属性节点的增加,删除,修改操作源码
- c#对xml多属性节点的增加,删除,修改操作源码
- dom4j修改,获取,增加xml中某个元素的属性值
- [代码]访问内存中的XML元素和元素属性(LINQ to XML)
- c#对xml多属性节点的增加,删除,修改操作源码
- tinyxml使用:创建XML,遍历打印xml文件 select操作,获取单个节点值,删除节点操作,修改节点操作,增加节点操作
- [代码]获取XML元素的值(LINQ to XML)
- dom4j修改,获取,增加xml中某个元素的属性值
- C# Linq To XML 读取XML,修改属性
- [代码]使用函数构造创建具有属性的元素(LINQ to XML)
- Asp.net中C#增加删除查找与修改xml文件元素节点
- LINQ To XML:遍历元素属性
- LinqToXML添加 XML元素 及修改属性值
- LinQ To Xml 通过元素属性检索元素的值
- jQuery元素属性操作实例(设置、获取及删除元素属性)