新手诚信请教asp.net c# 操作xml的问题公司要用asp.net做个小网站,我是搞JAVA的没办法硬头皮了更能是:加载一个XML文件在前台以表格的形式显示
比如test.xml
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright-->
<XunJZ-ZhangDX-990sv>
<Column>
<Home>
<Title>Home首页</Title>
<Section>
<Title>Welcome</Title>
<Abstract>This is welcome abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
</Home>
<About>
<Title>About Us</Title>
<Section>
<Title>Company Profile</Title>
<Abstract>This is Compan Profile abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
<Section>
<Title>What We Offer</Title>
<Abstract>This is What We Offer abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
</About>
<Product>
<Title>Product</Title>
<Section>
<Title>Product01</Title>
<Abstract>This is Product01 abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
<Section>
<Title>Product02</Title>
<Abstract>This is Product02 abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
</Product>
<Contact>
<Title>Contact Us</Title>
<Section>
<Title>Department</Title>
<Abstract>This is Department abstract</Abstract>
<Address>2206 #8 Bei Men Qiao Lu, NanJing, JiangSu, China</Address>
<Zip></Zip>
<Tel>
<List>025-84715231</List>
<List>025-84715232</List>
</Tel>
<Fax>
<List>025-84715231</List>
<List>025-84715232</List>
</Fax>
<Email>
<List>[email protected]</List>
<List>[email protected]</List>
</Email>
</Section>
</Contact>
</Column>
<Information>
<Developer>
<Title>Cool flash website</Title>
<Link>www.990sv.cn</Link>
<Copyright>
<Title>privacy policy</Title>
<Content>The cool flash website is developper by Xun Juezhang & Zhang Dingxin</Content>
</Copyright>
</Developer>
<Authorization>
<Title>The client website</Title>
<Link>www.domain.cn</Link>
<Copyright>
<Title>privacy policy</Title>
<Content>The cool flash website is used by www.domain.com</Content>
</Copyright>
</Authorization>
</Information>
<Extra>
<Template>Common/Template/Temp01.swf</Template>
<Music>
<Mp3>Common/Music/Mu1.mp3</Mp3>
<Mp3>Common/Music/Mu2.mp3</Mp3>
<Mp3>Common/Music/Mu3.mp3</Mp3>
</Music>
</Extra>
</XunJZ-ZhangDX-990sv>在前台显示所有数据,并且当双击某一行时能显示一个文本框在里面修改,修改后能保存到XML中。兄弟在次非常感谢了。谢谢各位不吝赐教啊
我想比较完整的前后台代码,例子也可以
比如test.xml
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright-->
<XunJZ-ZhangDX-990sv>
<Column>
<Home>
<Title>Home首页</Title>
<Section>
<Title>Welcome</Title>
<Abstract>This is welcome abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
</Home>
<About>
<Title>About Us</Title>
<Section>
<Title>Company Profile</Title>
<Abstract>This is Compan Profile abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
<Section>
<Title>What We Offer</Title>
<Abstract>This is What We Offer abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
</About>
<Product>
<Title>Product</Title>
<Section>
<Title>Product01</Title>
<Abstract>This is Product01 abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
<Section>
<Title>Product02</Title>
<Abstract>This is Product02 abstract</Abstract>
<Content>AMET INELIT LED DIAM AMSEHR LASLAORES LOEM IPSUM DOLOR SIT AMET INOREM KERODE SED DIAMASISMCI KERTSEHR LISLAORES JEAOLORE MAGNA IOLUTPAIM.</Content>
</Section>
</Product>
<Contact>
<Title>Contact Us</Title>
<Section>
<Title>Department</Title>
<Abstract>This is Department abstract</Abstract>
<Address>2206 #8 Bei Men Qiao Lu, NanJing, JiangSu, China</Address>
<Zip></Zip>
<Tel>
<List>025-84715231</List>
<List>025-84715232</List>
</Tel>
<Fax>
<List>025-84715231</List>
<List>025-84715232</List>
</Fax>
<Email>
<List>[email protected]</List>
<List>[email protected]</List>
</Email>
</Section>
</Contact>
</Column>
<Information>
<Developer>
<Title>Cool flash website</Title>
<Link>www.990sv.cn</Link>
<Copyright>
<Title>privacy policy</Title>
<Content>The cool flash website is developper by Xun Juezhang & Zhang Dingxin</Content>
</Copyright>
</Developer>
<Authorization>
<Title>The client website</Title>
<Link>www.domain.cn</Link>
<Copyright>
<Title>privacy policy</Title>
<Content>The cool flash website is used by www.domain.com</Content>
</Copyright>
</Authorization>
</Information>
<Extra>
<Template>Common/Template/Temp01.swf</Template>
<Music>
<Mp3>Common/Music/Mu1.mp3</Mp3>
<Mp3>Common/Music/Mu2.mp3</Mp3>
<Mp3>Common/Music/Mu3.mp3</Mp3>
</Music>
</Extra>
</XunJZ-ZhangDX-990sv>在前台显示所有数据,并且当双击某一行时能显示一个文本框在里面修改,修改后能保存到XML中。兄弟在次非常感谢了。谢谢各位不吝赐教啊
我想比较完整的前后台代码,例子也可以
http://www.dezai.cn/blog/article.asp?id=103XML语法结构总结
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=28669Asp.Net XML操作基类(修改,删除,新增,创建)
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=26244DataGrid 编辑 修改XML文件
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=24610使用 XML 文件记录操作日志
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=23437XML字符串的读写操作函数及示例程序
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=23885
读取xml节点的数据总结
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=32341XML对层级数据和HTML表格排序
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=20165C#操纵XML文档
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=19467XML封装数据库操作语句
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=19530对XML实现自定义、多字段排序
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=19811
XmlDocument xmldoc = new XmlDocument();
String tempstr1 = Path.Combine(Server.MapPath("."),"xml");
String xmlPath = Path.Combine(tempstr1,"news_subject.xml");
xmldoc.Load(xmlPath);
XmlElement e_item = xmldoc.CreateElement("subject");
XmlElement e_cell_id = xmldoc.CreateElement("lm_id");
XmlElement e_cell_name = xmldoc.CreateElement("lm_name");
e_cell_id.InnerText = "3";
e_cell_name.InnerText = "栏目3";
e_item.AppendChild(e_cell_name);
e_item.AppendChild(e_cell_id);
xmldoc.DocumentElement.AppendChild(e_item);
xmldoc.Save(xmlPath);
String tempstr1 = Path.Combine(Server.MapPath("."),"xml");
String xmlPath = Path.Combine(tempstr1,"news_subject.xml");
xmldoc.Load(xmlPath);
XmlNode delNode = xmldoc.SelectSingleNode("NewsSubject/subject[lm_id=3]");
xmldoc.DocumentElement.RemoveChild(delNode);
xmldoc.Save(xmlPath);
Response.Write("删除成功");
XmlDocument xmldoc = new XmlDocument();
String tempstr1 = Path.Combine(Server.MapPath("."),"xml");
String xmlPath = Path.Combine(tempstr1,"news_subject.xml");
xmldoc.Load(xmlPath);
XmlNodeList NdList = xmldoc.SelectNodes("NewsSubject/subject");
for(int i=0;i<NdList.Count;i++)
{
String lm_id = NdList[i].SelectSingleNode("lm_id").InnerXml.Trim();
String lm_name = "";
lm_name = NdList[i].SelectSingleNode("lm_name").InnerXml.Trim();
Response.Write(lm_name+"<br>"+lm_id+"<br>");
}
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnd;
String tempstr1 = Path.Combine(Server.MapPath("."),"xml");
String xmlPath = Path.Combine(tempstr1,"news_subject.xml");
xmldoc.Load(xmlPath);
xmlnd = xmldoc.SelectNodes("NewsSubject/subject");
//读xml [3/22/2004]
for(int i=0;i<xmlnd.Count;i++)
{
String lm_id = xmlnd[i].SelectSingleNode("lm_id").InnerXml.Trim();
String lm_name = xmlnd[i].SelectSingleNode("lm_name").InnerXml.Trim();
// modify [3/22/2004]
if(lm_id=="3")
{
XmlNode node = xmlnd[i].SelectSingleNode("lm_name");
node.InnerXml = lm_name+i.ToString()+"修该后。";
}
}
xmldoc.Save(xmlPath);
其实就是用:XmlReader 和 XmlWriter 去读写xml.好处就是不需要XmlDocument的一次性把整个xml文件load进来.
所以对于大的xml应该使用 XmlReader / XmlWriter 去读写xml.这个你可以自己去查一下.
XmlTextWriter xmlWriter = new XmlTextWriter( tw );
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.WriteStartElement("rss");
xmlWriter.WriteAttributeString("version", "2.0");
xmlWriter.WriteStartElement("channel");
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/Rss.aspx");
xmlWriter.WriteElementString("description", "NET开发技术。");
xmlWriter.WriteElementString("language", "zh-CN");
xmlWriter.WriteElementString("copyright", "Copyright 1999-2007【孟宪会之精彩世界】");
xmlWriter.WriteElementString("managingEditor", "amxh[AT]21cn.com");
xmlWriter.WriteStartElement("image");
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】");
xmlWriter.WriteElementString("width", "144");
xmlWriter.WriteElementString("height", "35");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/");
xmlWriter.WriteElementString("url", "http://dotnet.aspx.cc/Images/LogoRss.gif");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
xmlWriter.Close();
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader readData = XmlReader.Create(FileName, settings);
///用XML string 初始XmlReader
XmlTextReader xmlTextReader = new XmlTextReader( new StringReader( Content ) );
XmlReader reader = XmlReader.Create( xmlTextReader, new XmlReaderSettings() );
我需要将xml中的所有信息在页面上显示出来,随便怎么显示,只要方便修改就行
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright-->
<XunJZ-ZhangDX-990sv>
<Column>
<Home>
<Title>Home首页</Title>
<Section>
<Title>Welcome</Title>
<Abstract>摘要</Abstract>
<Content>详细内容</Content>
</Section>
</Home>
<About>
<Title>关于我们</Title>
<Section>
<Title>标题</Title>
<Abstract>摘要</Abstract>
<Content>详细信息</Content>
</Section>
<Section>
<Title>标题</Title>
<Abstract>摘要</Abstract>
<Content>详细信息</Content>
</Section>
</About>
<Product>
<Title>产品01</Title>
<Section>
<Title>标题</Title>
<Abstract>摘要</Abstract>
<Content>详细信息</Content>
</Section>
<Title>产品02</Title>
<Section>
<Title>标题</Title>
<Abstract>摘要</Abstract>
<Content>详细信息</Content>
</Section>
</Product>
<Contact>
<Title>联系我们</Title>
<Section>
<Title>标题</Title>
<Abstract>摘要</Abstract>
<Address>地址</Address>
<Zip>邮编</Zip>
<Tel>
<List>025-84715231</List>
<List>025-84715232</List>
</Tel>
<Fax>
<List>025-84715231</List>
<List>025-84715232</List>
</Fax>
<Email>
<List>[email protected]</List>
<List>[email protected]</List>
</Email>
</Section>
</Contact>
</Column>
<Information>
<Developer>
<Title>标题</Title>
<Link>www.990sv.cn</Link>
<Copyright>
<Title>标题</Title>
<Content>详细信息</Content>
</Copyright>
</Developer>
<Authorization>
<Title>标题</Title>
<Link>www.domain.cn</Link>
<Copyright>
<Title>标题</Title>
<Content>信息信息</Content>
</Copyright>
</Authorization>
</Information>
<Extra>
<Template>Common/Template/Temp01.swf</Template>
<Music>
<Mp3>Common/Music/Mu1.mp3</Mp3>
<Mp3>Common/Music/Mu2.mp3</Mp3>
<Mp3>Common/Music/Mu3.mp3</Mp3>
</Music>
</Extra>
</XunJZ-ZhangDX-990sv>
需要添加的命名空间:
using System.Xml;
定义几个公共对象:
XmlDocument xmldoc ;
XmlNode xmlnode ;
XmlElement xmlelem ;
1,创建到服务器同名目录下的xml文件: 方法一:
xmldoc = new XmlDocument ( ) ;
//加入XML的声明段落
xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;
xmldoc.AppendChild ( xmlnode ) ;
//加入一个根元素
xmlelem = xmldoc.CreateElement ( "" , "Employees" , "" ) ;
xmldoc.AppendChild ( xmlelem ) ;
//加入另外一个元素
for(int i=1;i<3;i )
{
XmlNode root=xmldoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=xmldoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElement xesub1=xmldoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2=xmldoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmldoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>节点中
}
//保存创建好的XML文档
xmldoc.Save ( Server.MapPath("data.xml") ) ;
//////////////////////////////////////////////////////////////////////////////////////
结果:在同名目录下生成了名为data.xml的文件,内容如下,
<?xml version="1.0"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees> 方法二:
XmlTextWriter xmlWriter;
string strFilename = Server.MapPath("data1.xml") ;
xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Employees");
xmlWriter.WriteStartElement("Node");
xmlWriter.WriteAttributeString("genre","李赞红");
xmlWriter.WriteAttributeString("ISBN","2-3631-4");
xmlWriter.WriteStartElement("title");
xmlWriter.WriteString("CS从入门到精通");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("author");
xmlWriter.WriteString("候捷");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("price");
xmlWriter.WriteString("58.3");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
//////////////////////////////////////////////////////////////////////////////////////
结果:
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>
2,添加一个结点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("data.xml"));
XmlNode root=xmlDoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=xmlDoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre","张三");//设置该节点genre属性
xe1.SetAttribute("ISBN","1-1111-1");//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="C#入门帮助";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="高手";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="158.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>节点中
xmlDoc.Save ( Server.MapPath("data.xml") );
//////////////////////////////////////////////////////////////////////////////////////
结果:在xml原有的内容里添加了一个结点,内容如下,
<?xml version="1.0"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
</Employees>
3,修改结点的值(属性和子结点):
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") );
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="张三")//如果genre属性值为“张三”
{
xe.SetAttribute("genre","update张三");//则修改该属性为“update张三”
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
}
}
}
}
xmlDoc.Save( Server.MapPath("data.xml") );//保存。
//////////////////////////////////////////////////////////////////////////////////////
结果:将原来的所有结点的信息都修改了,xml的内容如下,
<?xml version="1.0"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="update张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
</Node>
</Employees>
4,修改结点(添加结点的属性和添加结点的自结点):
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") );
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach(XmlNode xn in nodeList)
{
XmlElement xe=(XmlElement)xn;
xe.SetAttribute("test","111111");
XmlElement xesub=xmlDoc.CreateElement("flag");
xesub.InnerText="1";
xe.AppendChild(xesub);
}
xmlDoc.Save( Server.MapPath("data.xml") );
//////////////////////////////////////////////////////////////////////////////////////
结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下,
<?xml version="1.0"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
<flag>1</flag>
</Node>
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
<flag>1</flag>
</Node>
<Node genre="update张三" ISBN="1-1111-1" test="111111">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
<flag>1</flag>
</Node>
</Employees>
5,删除结点中的某一个属性:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") );
XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
xe.RemoveAttribute("genre");//删除genre属性
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="flag")//如果找到
{
xe.RemoveChild(xe2);//则删除
}
}
}
xmlDoc.Save( Server.MapPath("data.xml") );
//////////////////////////////////////////////////////////////////////////////////////]
结果:删除了结点的一个属性和结点的一个子结点,内容如下,
<?xml version="1.0"?>
<Employees>
<Node ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node ISBN="1-1111-1" test="111111">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
</Node>
</Employees>
6,删除结点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") );
XmlNode root=xmlDoc.SelectSingleNode("Employees");
XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes;
for(int i=0;i<xnl.Count;i )
{
XmlElement xe=(XmlElement)xnl.Item(i);
if(xe.GetAttribute("genre")=="张三")
{
root.RemoveChild(xe);
if(i<xnl.Count)i=i-1;
}
}
xmlDoc.Save( Server.MapPath("data.xml") );
结果:删除了符合条件的所有结点,原来的内容:
<?xml version="1.0"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
</Employees>
删除后的内容:
<?xml version="1.0"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>