在线急等:求C#操作xml文件方法 求:向xml 文件中增加,删除,获取某一 节点值的代码急QQ:10714155 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考http://blog.csdn.net/lizanhong/archive/2004/06/23/24374.aspx <?xml version="1.0" encoding="utf-8"?><configuration> <project> <name>Default</name> <bugtype>Work Item#Test Issue</bugtype> <role>Manager#Developer#Tester</role> <bugprority>High#Normal#Low</bugprority> </project></configuration>static string CONFIGFILE = System.Web.HttpContext.Current.Server.MapPath(@"ProjectConfig\project.xml");const string ROLE = "role";const string PRORITY = "bugprority";const string TYPE = "bugtype";----------------Read--------------------------------private static IList GetXmlNodeValue( string nodeName, string prjName ){ if( System.IO.File.Exists( CONFIGFILE ) ) { XmlDocument doc = new XmlDocument(); doc.Load( CONFIGFILE ); XmlNode node = doc.DocumentElement.SelectSingleNode( "//project/" + nodeName + "/text()[../../name='" + prjName + "']"); return node.Value.Split('#'); } else { return null; }} ------------------Write----------------------------------public static void WriteXml( string prjName, ArrayList type, ArrayList role, ArrayList prority ){ if( System.IO.File.Exists( CONFIGFILE ) ) { XmlDocument doc = new XmlDocument(); doc.Load( CONFIGFILE ); XmlElement project = doc.CreateElement("project"); XmlElement name = doc.CreateElement("name"); name.InnerText = prjName; XmlElement bugtype = doc.CreateElement("bugtype"); bugtype.InnerText = PrepareString( type ); XmlElement userRole = doc.CreateElement("role"); userRole.InnerText = PrepareString( role ); XmlElement bugprority = doc.CreateElement("bugprority"); bugprority.InnerText = PrepareString( prority ); project.AppendChild( name ); project.AppendChild( bugtype ); project.AppendChild( userRole ); project.AppendChild( bugprority ); doc.DocumentElement.AppendChild( project ); lock( doc ) { doc.Save( CONFIGFILE ); } }}---------------------------Delete----------------------------------------public static void DeleteProjectConfiguration( string prjName ){ if( System.IO.File.Exists( CONFIGFILE ) ) { XmlDocument doc = new XmlDocument(); doc.Load( CONFIGFILE ); XmlNode projectNode = doc.DocumentElement.SelectSingleNode( "//project[name='" + prjName + "']" ); doc.DocumentElement.RemoveChild( projectNode ); lock( doc ) { doc.Save( CONFIGFILE ); } }} 使用SelectSingleNode就可以读到所有的节点,跟注册表操作一样。 XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);...//修改内容xmlDoc.Save(filepath);上面都有的 XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);XmlNodeList xnlTab = xmlDoc.GetElementsByTagName("id");//id为要读取的节点名XmlElement xeTab = (XmlElement)xnlTab[0];//第一个id节点string id_name = xeTab.GetAttribute("name");//获取其name属性值string id_text = xeTab.InnerText;//获取id的节点值...xmlDoc.Save(filepath); 可不可以给代码看看!如果是多行的怎么读取某个值呀,文件如下 <?xml version="1.0" encoding="GB2312" ?> - <Goods_List>- <products>- <Product> <style>购销</style> <zuhe>加工商品(拆分)</zuhe> <Tax>停用</Tax> <Returnable>是</Returnable> <Supply_Type>是</Supply_Type> <Per_Package>否</Per_Package> <Barcode>否</Barcode> <Reg_Price>u</Reg_Price> <Cost>u</Cost> <Unit>u</Unit> <Pack_Size>u</Pack_Size> <Description>uy</Description> <No>000001</No> <HQ_Order>u</HQ_Order> <Sale_Allowed>u</Sale_Allowed> <Order_Allowed>u</Order_Allowed> <Stock_Book>u</Stock_Book> <Category>u</Category> <Layer>u</Layer> <Code>u</Code> <Attribute>u</Attribute> <Brand>是</Brand> <Mandatory>是</Mandatory> <Minimum_Order>u</Minimum_Order> </Product>+ <Product> <style>购销</style> <zuhe>加工商品(拆分)</zuhe> <Tax>停用</Tax> <Returnable>是</Returnable> <Supply_Type>否</Supply_Type> <Per_Package>否</Per_Package> <Barcode>是</Barcode> <Reg_Price>90</Reg_Price> <Cost>90</Cost> <Unit>9</Unit> <Pack_Size>9</Pack_Size> <Description>9</Description> <No>0000019</No> <Category /> <Layer /> <Code /> <HQ_Order /> <Sale_Allowed /> <Order_Allowed /> <Stock_Book /> <Attribute /> <Brand /> <Mandatory /> <Minimum_Order /> </Product> </products> </Goods_List>我想读 no 的值,怎么读怎么删除?????天啦 在http://blog.csdn.net/lizanhong/archive/2004/06/23/24374.aspx有对xml文件的基本操作 找了个综合一点的。文章太长了,你自己去看看吧:http://blog.csdn.net/gztoby/archive/2004/08/13/74219.aspx 在C#中操作XML 已知有一个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, Eva</author> <price>5.95</price> </book> <book genre="李赞红" ISBN="2-3631-4"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </book></bookstore> 2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。 XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点 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="亚胜";//则修改 break;//找到退出来就可以了 } } break; } } 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, Eva</author> <price>5.95</price> </book> <book genre="update李赞红" ISBN="2-3631-4"> <title>CS从入门到精通</title> <author>亚胜</author> <price>58.3</price> </book></bookstore> 3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes; foreach(XmlNode xn in xnl) { XmlElement xe=(XmlElement)xn; if(xe.GetAttribute("genre")=="fantasy") { xe.RemoveAttribute("genre");//删除genre属性 } else if(xe.GetAttribute("genre")=="update李赞红") { xe.RemoveAll();//删除该节点的全部内容 } } xmlDoc.Save("bookstore.xml");//===========================================最后结果为:<?xml version="1.0" encoding="gb2312"?><bookstore> <book ISBN="2-3631-4"> <title>Oberon's Legacy</title> <author>Corets, Eva</author> <price>5.95</price> </book> <book> </book></bookstore> 4、显示所有数据。 XmlNode xn=xmlDoc.SelectSingleNode("bookstore"); XmlNodeList xnl=xn.ChildNodes; foreach(XmlNode xnf in xnl) { XmlElement xe=(XmlElement)xnf; Console.WriteLine(xe.GetAttribute("genre"));//显示属性值 Console.WriteLine(xe.GetAttribute("ISBN")); XmlNodeList xnf1=xe.ChildNodes; foreach(XmlNode xn2 in xnf1) { Console.WriteLine(xn2.InnerText);//显示子节点点文本 } 在win 7下,有时候模式化窗体会显示在父窗体后面? DataSet序列化与反序列化问题,高手帮忙--在线急等 财付通安全控件,如何自动输入密码? 请问一个ado.net的问题 BOXING的问题 void fun(object param) 水晶报表中如何动态连接数据库? ckeditor 上传图片 pictureBox滚动 128MB内存能不能玩得起C#??? 在层级列表中如何查找并删除指定对象 winform中xml的操作 window程序界面涉及的问题
http://blog.csdn.net/lizanhong/archive/2004/06/23/24374.aspx
<configuration>
<project>
<name>Default</name>
<bugtype>Work Item#Test Issue</bugtype>
<role>Manager#Developer#Tester</role>
<bugprority>High#Normal#Low</bugprority>
</project>
</configuration>
static string CONFIGFILE = System.Web.HttpContext.Current.Server.MapPath(@"ProjectConfig\project.xml");
const string ROLE = "role";
const string PRORITY = "bugprority";
const string TYPE = "bugtype";----------------Read--------------------------------
private static IList GetXmlNodeValue( string nodeName, string prjName )
{
if( System.IO.File.Exists( CONFIGFILE ) )
{
XmlDocument doc = new XmlDocument();
doc.Load( CONFIGFILE );
XmlNode node = doc.DocumentElement.SelectSingleNode(
"//project/" +
nodeName +
"/text()[../../name='" +
prjName + "']");
return node.Value.Split('#');
}
else
{
return null;
}
}
public static void WriteXml( string prjName, ArrayList type, ArrayList role, ArrayList prority )
{
if( System.IO.File.Exists( CONFIGFILE ) )
{
XmlDocument doc = new XmlDocument();
doc.Load( CONFIGFILE );
XmlElement project = doc.CreateElement("project");
XmlElement name = doc.CreateElement("name");
name.InnerText = prjName;
XmlElement bugtype = doc.CreateElement("bugtype");
bugtype.InnerText = PrepareString( type ); XmlElement userRole = doc.CreateElement("role");
userRole.InnerText = PrepareString( role ); XmlElement bugprority = doc.CreateElement("bugprority");
bugprority.InnerText = PrepareString( prority ); project.AppendChild( name );
project.AppendChild( bugtype );
project.AppendChild( userRole );
project.AppendChild( bugprority ); doc.DocumentElement.AppendChild( project );
lock( doc )
{
doc.Save( CONFIGFILE );
}
}
}
---------------------------Delete----------------------------------------
public static void DeleteProjectConfiguration( string prjName )
{
if( System.IO.File.Exists( CONFIGFILE ) )
{
XmlDocument doc = new XmlDocument();
doc.Load( CONFIGFILE );
XmlNode projectNode = doc.DocumentElement.SelectSingleNode( "//project[name='" + prjName + "']" );
doc.DocumentElement.RemoveChild( projectNode );
lock( doc )
{
doc.Save( CONFIGFILE );
}
}
}
xmlDoc.Load(filepath);
...//修改内容
xmlDoc.Save(filepath);上面都有的
xmlDoc.Load(filepath);XmlNodeList xnlTab = xmlDoc.GetElementsByTagName("id");//id为要读取的节点名
XmlElement xeTab = (XmlElement)xnlTab[0];//第一个id节点
string id_name = xeTab.GetAttribute("name");//获取其name属性值
string id_text = xeTab.InnerText;//获取id的节点值
...
xmlDoc.Save(filepath);
如果是多行的怎么读取某个值呀,文件如下
<?xml version="1.0" encoding="GB2312" ?>
- <Goods_List>
- <products>
- <Product>
<style>购销</style>
<zuhe>加工商品(拆分)</zuhe>
<Tax>停用</Tax>
<Returnable>是</Returnable>
<Supply_Type>是</Supply_Type>
<Per_Package>否</Per_Package>
<Barcode>否</Barcode>
<Reg_Price>u</Reg_Price>
<Cost>u</Cost>
<Unit>u</Unit>
<Pack_Size>u</Pack_Size>
<Description>uy</Description>
<No>000001</No>
<HQ_Order>u</HQ_Order>
<Sale_Allowed>u</Sale_Allowed>
<Order_Allowed>u</Order_Allowed>
<Stock_Book>u</Stock_Book>
<Category>u</Category>
<Layer>u</Layer>
<Code>u</Code>
<Attribute>u</Attribute>
<Brand>是</Brand>
<Mandatory>是</Mandatory>
<Minimum_Order>u</Minimum_Order>
</Product>
+ <Product>
<style>购销</style>
<zuhe>加工商品(拆分)</zuhe>
<Tax>停用</Tax>
<Returnable>是</Returnable>
<Supply_Type>否</Supply_Type>
<Per_Package>否</Per_Package>
<Barcode>是</Barcode>
<Reg_Price>90</Reg_Price>
<Cost>90</Cost>
<Unit>9</Unit>
<Pack_Size>9</Pack_Size>
<Description>9</Description>
<No>0000019</No>
<Category />
<Layer />
<Code />
<HQ_Order />
<Sale_Allowed />
<Order_Allowed />
<Stock_Book />
<Attribute />
<Brand />
<Mandatory />
<Minimum_Order />
</Product>
</products>
</Goods_List>我想读 no 的值,怎么读
怎么删除?????
天啦
http://blog.csdn.net/lizanhong/archive/2004/06/23/24374.aspx
有对xml文件的基本操作
已知有一个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, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
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="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
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, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");
//===========================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、显示所有数据。
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}