C#中如何修改xml文件中子获取xml节点的值文本值

Qt中修改XML文件中的节点值
来源:csdn
【不能修改XML文件的节点
我写的源码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
QFile *file=new QFile("/home/qust/qt/XML/2.xml");
if(file->open(QIODevice::ReadOnly|QIODevice::WriteOnly))
dom.setContent(file);
QDomNodeList email=dom.documentElement().elementsByTagName("email");
qDebug()<<email.count();//打印 1
qDebug()<close();
MainWindow::~MainWindow()
不能修改XML文件,运行后打开xml文件出现
该怎么修改xml 中节点的值呢??为什么不能使用setNodeValue()方法来修改接点的值???
大家帮帮忙,谢谢!!!】
myseemydog:
中间是TextNode。你修改它的值。
setNodeValue 不起作用
一般是操作节点的问题.
FounderSG:
dom.setContent(file);
这一行就出错了吧,你的2.xml是不是有问题?
2.xml 是我qt写的
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include &iostream&
#include &QTextStream&
#include &QDebug&
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui-&setupUi(this);
QFile *file=new QFile("/home/qust/qt/XML/2.xml");
if(!file-&open(QIODevice::WriteOnly|QIODevice::ReadOnly))
QDomProcessingIns
instruction=doc.createProcessingInstruction("xml","version=\'1.0\'");
doc.appendChild(instruction);
QDomElement root=doc.createElement("kdevelop");
doc.appendChild(root);
QDomElement general=doc.createElement("general");
root.appendChild(general);
element=doc.createElement("author");
text=doc.createTextNode("zeki");
element.appendChild(text);
general.appendChild(element);
element=doc.createElement("email");
text=doc.createTextNode("");
element.appendChild(text);
general.appendChild(element);
QTextStream out(file);
doc.save(out,4);
file-&close();
MainWindow::~MainWindow()
#include &QtGui/QApplication&
#include "mainwindow.h"
#include &QDialog&
#include &QFile&
#include &QDebug&
#include &QDomDocument&
#include &QFile&
QDomDocument m_
changeSave();
bool openXmlFile(QString FilePath);
int main(int argc, char *argv[])
QApplication a(argc, argv);
changeSave();
return a.exec();
bool openXmlFile(QString FilePath)
QFile file( FilePath );
if( !file.open( QFile::ReadOnly | QFile::Text
qDebug() && QObject::tr("error::ParserXML-&OpenXmlFile-&file.open-&%s\n") && FileP
if( !m_doc.setContent( &file ) )
qDebug() && QObject::tr("error::ParserXML-&OpenXmlFile-&doc.setContent\n") && FileP
file.close();
file.close();
changeSave()
if(!openXmlFile("I:/q.xml"))
//修改保存xml
QDomElement root = m_doc.documentElement();
if(root.tagName()!= "kdevelop")
QDomNode n = root.firstChild();
while ( !n.isNull() )
QDomElement e = n.toElement();
if( !e.isNull())
if( e.nodeName() == "general" )
QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
for(int a=0; a&list.count(); a++) //遍历该列表
QDomNode node = list.at(a);
if(node.isElement())
if( node.nodeName() == "author" )
QDomNode oldnode = node.firstChild();
//标签之间的内容作为节点的子节点出现,得到原来的子节点
node.firstChild().setNodeValue("csdn");
//用提供的value值来设置子节点的内容
QDomNode newnode = node.firstChild();
//值修改过后
node.replaceChild(newnode,oldnode);
//调用节点的replaceChild方法实现修改功能
if( node.nodeName() == "email" )
QDomNode oldnode = node.firstChild();
node.firstChild().setNodeValue("");
QDomNode newnode = node.firstChild();
node.replaceChild(newnode,oldnode);
n = n.nextSibling();
QFile filexml("I:/q.xml");
if( !filexml.open( QFile::WriteOnly | QFile::Truncate) ){
qWarning("error::ParserXML-&writeOperateXml-&file.open\n");
QTextStream ts(&filexml);
ts.reset();
ts.setCodec("utf-8");
m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
filexml.close();
用到了,谢谢分享
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
本人写博客只是为了方便以后查找方便,如果对大家有帮助那是最好不过的,如果没有帮助到大家或者写的不对,请勿喷,如果写的不对,而你又愿意给我正确答案,请回复我正确答案,谢谢!
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(6236)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'C#中如何操作XML,对XML进行增、删、改节点',
blogAbstract:'增加节点1.读取XML文件(加载XML文件)&XmlDocument doc = new XmlDocument();&doc.Load(\"student.xml\");2.查找根节点&XmlNode xn = doc.SelectSingleNode(\"student\");3.创建子节点student&XmlElement node = doc.CreateElement(\"student\");4.创建节点name、age、gender&XmlElement name = doc.CreateElement(\"name\");&name.InnerText = \"kunkun\";&XmlElement age = doc.CreateElement(\"age\");',
blogTag:'c#中如何操作xml,对xml进行增、删、改节点',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:4,
permalink:'blog/static/',
commentCount:1,
mainCommentCount:1,
recommendCount:1,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'本人写博客只是为了方便以后查找方便,如果对大家有帮助那是最好不过的,如果没有帮助到大家或者写的不对,请勿喷,如果写的不对,而你又愿意给我正确答案,请回复我正确答案,谢谢!',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}C#中用XmlDocument对象获取XML文件中的节点值
XmlDocument是表示DOM的类。
1.加载XML文档:使用load()方法加载XML文档;
2.读取节点:使用GetElementById()、getElementsByTagName_r()方法根据ID或标签名读取节点;
3.查找节点:使用SelectSingleNode(string search)方法通过XPath查找节点;
4.插入节点:使用createElement_x()方法创建节点,AppendChild()方法添加新节点;
5.创建文档:通过XmlDeclaration对象新建声明节点,其他同插入节点。
&?xml version=&1.0& encoding=&utf-8& ?&
&bookstore&
&&& &title lang=&eng&&Harry Potter&/title&
&&& &price&29.99&/price&
&&& &title lang=&eng&&Learning XML&/title&
&&& &price&39.98&/price&
& &bookstore&
&&& &title lang=&eng&&Learning C#&/title&
&&& &price&55.23&/price&
& &/bookstore&
& &item name=&1&&第一个item&/item&
& &item name=&2&&
&&& &item name=&1&&这个结点(1)&/item&
&&& &item name=&2&&这个结点(2)&/item&
&&& &book&
&&&&& &title lang=&cn&&Learning C&/title&
&&&&& &price&60.95&/price&
&&& &/book&
&/bookstore&
&&&&&&&&&& XmlDocument xdoc = new XmlDocument();
&&&&&&&&&&& xdoc.Load(Server.MapPath(&books.xml&));
&&&&&&&&&&& XmlNodeList xnl = xdoc.SelectNodes(&/bookstore/book&);
&&&&&&&&&&& string nodeTempStr = &&;
&&&&&&&&&&& foreach (XmlNode node in xnl)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& nodeTempStr = node.InnerT
&&&&&&&&&&&&&&& node ......
实例:处理books.xml文档
TestXmlDocument.cs:
001.using S
002.using System.X
004.namespace Magci.Test.XML.TestXmlDocment
006.class Program
008.private static XmlD
010.static void Main(string[] args)
012.doc = new XmlDocument();
013.//加载XML文档
014.doc.Load(@&..\..\books.xml&);
015.DisplayTitle();
016.SearchByTitle(&The Gorgias&);
017.Insert();
018.CreateDoc();
020.Console.ReadLine();
023.//遍历节点
024.public static void DisplayTitle()
026.XmlNodeList nodes = doc.getElementsByTagName_r(&title&);
027.Console.WriteLine(&Title:&);
028.foreach (XmlNode node in nodes)
030.Console.WriteLine(node.InnerText);
034.//根据title查找
035.public static void SearchByTitle(string title)
037.string search = &bookstore/book[title=&#39;& + title + &&#39;]&;
038.XmlNode foundNode = doc.SelectSingleNode(search);
039.Console.WriteLine(&\nSearch by title:&);
040.if (foundNode != null)
042.Console.WriteLine(foundNode.InnerText);
046.Console.WriteLine(&Not Found!&);
050.//插入节点
051.public static void Insert()
053.//创建book元素
054.XmlElement newBook = doc.createElement_x(&book&);
055.newBook.SetAttribute(&genre&, &MyStery&);
056.newBook.SetAttribute(&publicationdate&, &2001&);
057.newBook.SetAttribute(&ISBN&, &&);
059.//创建title元素
060.XmlElement newTitle = doc.createElement_x(&title&);
061.newTitle.InnerText = &The Case of the Missing Cookie&;
062.//将title元素添加到book元素中
063.newBook.AppendChild(newTitle);
065.XmlElement newAuthor = doc.createElement_x(&author&);
066.newBook.AppendChild(newAuthor);
068.XmlElement newName = doc.createElement_x(&name&);
069.newName.InnerText = &C.Monster&;
070.newAuthor.AppendChild(newName);
072.XmlElement newPrice = doc.createElement_x(&price&);
073.newPrice.InnerText = &9.99&;
074.newBook.AppendChild(newPrice);
076.//将新建的book元素加入到XML文档中
077.doc.DocumentElement.AppendChild(newBook);
079.//另存为
080.XmlTextWriter tr = new XmlTextWriter(@&..\..\booksEdit.xml&, null);
081.tr.Formatting = Formatting.I
082.doc.WriteContentTo(tr);
083.tr.Close();
084.Console.WriteLine(&\nbooksEdit.xml Saved successful.\n&);
086.XmlNodeList nodes = doc.getElementsByTagName_r(&title&);
087.Console.WriteLine(&Display title after Insert:&);
088.foreach (XmlNode node in nodes)
090.Console.WriteLine(node.InnerText);
094.//创建文档
095.public static void CreateDoc()
097.XmlDocument newDoc = new XmlDocument();
099.//创建声明节点
100.XmlDeclaration newDec = newDoc.CreateXmlDeclaration(&1.0&, &utf-8&, null);
101.newDoc.AppendChild(newDec);
103.XmlElement newRoot = newDoc.createElement_x(&bookstore&);
104.newDoc.AppendChild(newRoot);
106.XmlElement newBook = newDoc.createElement_x(&book&);
107.newBook.SetAttribute(&genre&, &MyStery&);
108.newBook.SetAttribute(&publicationdate&, &2001&);
109.newBook.SetAttribute(&ISBN&, &&);
111.XmlElement newTitle = newDoc.createElement_x(&title&);
112.newTitle.InnerText = &The Case of the Missing Cookie&;
113.newBook.AppendChild(newTitle);
115.XmlElement newAuthor = newDoc.createElement_x(&author&);
116.newBook.AppendChild(newAuthor);
118.XmlElement newName = newDoc.createElement_x(&name&);
119.newName.InnerText = &C.Monster&;
120.newAuthor.AppendChild(newName);
122.XmlElement newPrice = newDoc.createElement_x(&price&);
123.newPrice.InnerText = &9.99&;
124.newBook.AppendChild(newPrice);
126.newRoot.AppendChild(newBook);
128.XmlTextWriter tr = new XmlTextWriter(@&..\..\booksCreate.xml&, null);
129.tr.Formatting = Formatting.I
130.newDoc.WriteContentTo(tr);
131.tr.Close();
132.Console.WriteLine(&\nbooksCreate.xml Saved successful.\n&);
134.XmlNodeList nodes = newDoc.getElementsByTagName_r(&title&);
135.Console.WriteLine(&Display title after Create:&);
136.foreach (XmlNode node in nodes)
138.Console.WriteLine(node.InnerText);
books.xml:
01.&?xml version=&1.0& encoding=&utf-8& ?&
02.&bookstore&
03.&book genre=&autobiography& publicationdate=&1991& ISBN=&1--0&&
04.&title&The Autobiography of Benjamin Franklin&/title&
05.&author&
06.&first-name&Benjamin&/first-name&
07.&last-name&Franklin&/last-name&
08.&/author&
09.&price&8.99&/price&
10.&/book&
11.&book genre=&novel& publicationdate=&1967& ISBN=&0-201-63361-2&&
12.&title&The Confidence Man&/title&
13.&author&
14.&first-name&Herman&/first-name&
15.&last-name&Melville&/last-name&
16.&/author&
17.&price&11.99&/price&
18.&/book&
19.&book genre=&philosophy& publicationdate=&1991& ISBN=&1--6&&
20.&title&The Gorgias&/title&
21.&author&
22.&name&Plato&/name&
23.&/author&
24.&price&9.99&/price&
25.&/book&
26.&/bookstore&
booksEdit.xml:修改后的XML文档
01.&?xml version=&1.0& encoding=&utf-8&?&
02.&bookstore&
03.&book genre=&autobiography& publicationdate=&1991& ISBN=&1--0&&
04.&title&The Autobiography of Benjamin Franklin&/title&
05.&author&
06.&first-name&Benjamin&/first-name&
07.&last-name&Franklin&/last-name&
08.&/author&
09.&price&8.99&/price&
10.&/book&
11.&book genre=&novel& publicationdate=&1967& ISBN=&0-201-63361-2&&
12.&title&The Confidence Man&/title&
13.&author&
14.&first-name&Herman&/first-name&
15.&last-name&Melville&/last-name&
16.&/author&
17.&price&11.99&/price&
18.&/book&
19.&book genre=&philosophy& publicationdate=&1991& ISBN=&1--6&&
20.&title&The Gorgias&/title&
21.&author&
22.&name&Plato&/name&
23.&/author&
24.&price&9.99&/price&
25.&/book&
26.&book genre=&MyStery& publicationdate=&2001& ISBN=&&&
27.&title&The Case of the Missing Cookie&/title&
28.&author&
29.&name&C.Monster&/name&
30.&/author&
31.&price&9.99&/price&
32.&/book&
33.&/bookstore&
booksCreate.xml:新建的XML文档
01.&?xml version=&1.0& encoding=&utf-8&?&
02.&bookstore&
03.&book genre=&MyStery& publicationdate=&2001& ISBN=&&&
04.&title&The Case of the Missing Cookie&/title&
05.&author&
06.&name&C.Monster&/name&
07.&/author&
08.&price&9.99&/price&
09.&/book&
10.&/bookstore&
摘自 wanglei_smartfish的专栏
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'西西软件园多重安全检测下载网站、值得信赖的软件下载站!
→ C#操作XML方法:新增、修改和删除节点与属性
类型:下载工具大小:828KB语言:中文 评分:7.5
一 前言先来了解下操作XML所涉及到的几个类及之间的关系 &如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家* 1 XMLElement 主要是针对节点的一些属性进行操作* 2 XMLDocument 主要是针对节点的CUID操作* 3 XMLNode 为抽象类,做为以上两类的基类,提供一些操作节点的方法清楚了以上的关系在操作XML时会更清晰一点二 具体操作以下会对Xml的结点与属性做增 删 改 查的操作也满足了实际工作中的大部分情况&先构造一棵XML树如下,其中也涉及到了写入xml文档的操作 1
public void CreatXmlTree(string xmlPath)
XElement xElement = new XElement(
new XElement(&BookStore&,
new XElement(&Book&,
new XElement(&Name&, &C#入门&, new XAttribute(&BookName&, &C#&)),
new XElement(&Author&, &Martin&, new XAttribute(&Name&, &Martin&)),
new XElement(&Adress&, &上海&),
new XElement(&Date&, DateTime.Now.ToString(&yyyy-MM-dd&))
new XElement(&Book&,
new XElement(&Name&, &WCF入门&, new XAttribute(&BookName&, &WCF&)),
new XElement(&Author&, &Mary&, new XAttribute(&Name&, &Mary&)),
new XElement(&Adress&, &北京&),
new XElement(&Date&, DateTime.Now.ToString(&yyyy-MM-dd&))
//需要指定格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encg = new UTF8Encoding(false);
settings.Indent =
XmlWriter xw = XmlWriter.Create(xmlPath,settings);
xElement.Save(xw);
//写入文件
xw.Flush();
xw.Close();
}然后得到如下的XML树 1 &?xml version=&1.0& encoding=&utf-8&?&
2 &BookStore&
&Name BookName=&C#&&C#入门&/Name&
&Author Name=&Martin&&Martin&/Author&
&Date&&/Date&
&Adress&上海&/Adress&
&Date&&/Date&
&Name BookName=&WCF&&WCF入门&/Name&
&Author Name=&Mary&&Mary&/Author&
&Adress&北京&/Adress&
&Date&&/Date&
16 &/BookStore&以下操作都是对生成的XML树进行操作2.1 新增节点与属性新增节点NewBook并增加属性Name=&WPF& 1
public void Create(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
var root = xmlDoc.DocumentE//取到根结点
XmlNode newNode = xmlDoc.CreateNode(&element&, &NewBook&, &&);
newNode.InnerText = &WPF&;
//添加为根元素的第一层子结点
root.AppendChild(newNode);
xmlDoc.Save(xmlPath);
}开篇有写操作xml节点属性主要用XmlElement对象所以取到结点后要转类型& 1
public void CreateAttribute(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
var root = xmlDoc.DocumentE//取到根结点
XmlElement node = (XmlElement)xmlDoc.SelectSingleNode(&BookStore/NewBook&);
node.SetAttribute(&Name&, &WPF&);
xmlDoc.Save(xmlPath);
}效果如下2.2 删除节点与属性 1
public void Delete(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
var root = xmlDoc.DocumentE//取到根结点
var element = xmlDoc.SelectSingleNode(&BookStore/NewBook&);
root.RemoveChild(element);
xmlDoc.Save(xmlPath);
}删除属性 1
public void DeleteAttribute(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement node = (XmlElement)xmlDoc.SelectSingleNode(&BookStore/NewBook&);
//移除指定属性
node.RemoveAttribute(&Name&);
//移除当前节点所有属性,不包括默认属性
//node.RemoveAllAttributes();
xmlDoc.Save(xmlPath);
}2.3 修改节点与属性xml的节点默认是不允许修改的,本文也就不做处理了& 修改属性代码如下1
public void ModifyAttribute(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement element = (XmlElement)xmlDoc.SelectSingleNode(&BookStore/NewBook&);
element.SetAttribute(&Name&, &Zhang&);
xmlDoc.Save(xmlPath);
}效果如下2.4 获取节点与属性 1
public void Select(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
//取根结点
var root = xmlDoc.DocumentE//取到根结点
//取指定的单个结点
XmlNode oldChild = xmlDoc.SelectSingleNode(&BookStore/NewBook&);
//取指定的结点的集合
XmlNodeList nodes = xmlDoc.SelectNodes(&BookStore/NewBook&);
//取到所有的xml结点
XmlNodeList nodelist = xmlDoc.GetElementsByTagName(&*&);
public void SelectAttribute(string xmlPath)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlElement element = (XmlElement)xmlDoc.SelectSingleNode(&BookStore/NewBook&);
string name = element.GetAttribute(&Name&);
Console.WriteLine(name);
}三 &linq to XML 操作&Linq to Xml 也没什么变化只操作对象改变了主要涉及的几个对象如下 & 注:我并没有用linq的语法去操作元素。XDocument:用于创建一个XML实例文档XElement:用于一些节点与节点属性的基本操作以下是对Xml的 一些简单的操作3.1 新增节点与属性1
public void Create(string xmlPath)
XDocument xDoc = XDocument.Load(xmlPath);
XElement xElement = xDoc.Element(&BookStore&);
xElement.Add(new XElement(&Test&, new XAttribute(&Name&, &Zery&)));
xDoc.Save(xmlPath);
public void CreateAttribute(string xmlPath)
XDocument xDoc = XDocument.Load(xmlPath);
IEnumerable&XElement& xElement = xDoc.Element(&BookStore&).Elements(&Book&);
foreach (var element in xElement)
element.SetAttributeValue(&Name&, &Zery&);
xDoc.Save(xmlPath);
}3.2 删除节点与属性1
public void Delete(string xmlPath)
XDocument xDoc = XDocument.Load(xmlPath);
XElement element = (XElement)xDoc.Element(&BookStore&).Element(&Book&);
element.Remove();
xDoc.Save(xmlPath);
public void DeleteAttribute(string xmlPath)
XDocument xDoc = XDocument.Load(xmlPath);
//不能跨级取节点
XElement element = xDoc.Element(&BookStore&).Element(&Book&).Element(&Name&);
element.Attribute(&BookName&).Remove();
xDoc.Save(xmlPath);
}3.3 修改节点属性节点.net没提供修改的方法本文也不做处理修改属性与新增实质是同一个方法1
public void ModifyAttribute(string xmlPath)
XDocument xDoc = XDocument.Load(xmlPath);
XElement element = xDoc.Element(&BookStore&).Element(&Book&);
element.SetAttributeValue(&BookName&,&ZeryTest&);
xDoc.Save(xmlPath);
}四 总结把文章写完时,又扫去了自己的一个盲区,虽然都是些简单的操作,但在实际的开中,又何尝不是由简单到复杂呢。我觉得身为程序员就应该遇到自己的盲区时,立马花时间去了解,不说要了解多深入,但至少基本的还是要知道,等到工作中真需时,只要稍微花点时间就可以了。
04-2504-2304-1503-1301-2801-2612-3108-3008-1906-24
阅读本文后您有什么感想? 已有23人给出评价!
名称大小下载

我要回帖

更多关于 获取xml节点的值 的文章

 

随机推荐