C#操作xml文件入门(后附示例源代码)
~/ 在runat=server的控件中,自动的被解析为Request.ApplicationPath的值,是当前应用程序级程序的目录 在例子中是:/WebSite
./或者什么都不写:表示当前目录,./teacup.jpg和teacup.jpg都表示当前网页所在目录下的teacup.jpg文件
../表示上一层目录,比如http://www.cnblogs.com/../teacup.jpg就表示当前网页所在目录的上三层的目录下的一个teacup.jpg文件,
/表示根目录,一般表示为:系统盘下的Inetpub\wwwroot
已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
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);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
//================
结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, E
相关文档:
java代码
package com.xml.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class XMLAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String username;
private String mobile;
public void setUs ......
文档及代码下载:http://www.dingos.cn/index.php?topic=1899.0
介绍
当菜单项是经常改变时,从XML文档中创建TreeView才当是非常有用的。例如,使用XML作为数据库存储记录。
这里有个简单示例。以防混淆这个例子比较简单。
背景
对XML、TreeView控件的应用,在Visual Studio中有助于理解这些步骤。
使用代码
注意 ......
VC解析XML--使用CMarkup类解析XML
(一) 先讲一下XML中的物殊字符,手动填写时注意一下。
字符 字符实体
& &nb ......
源xml文件
<?xml version="1.0" encoding="iso-8859-1"?>
<books>
<stock>
<name>The Picasso Code</name>
<author>Dan Blue</author>
<category>Fiction</category>
<description>Cubist paintings reveal a secret society of people ......