XML基本读,写,删除操作
来源:互联网 发布:linux chown 编辑:程序博客网 时间:2024/05/22 02:10
XML 数据格式如下:
<?xml version="1.0" encoding="utf-8"?>
<PeopleInfo>
<PeopleInfoList str_Epc="350000000000000000000000" str_name="chpdirect">
<str_PeopleID>peopleid0000</str_PeopleID>
<str_PeopleName>peoplename0000</str_PeopleName>
<str_PeopleUnit>11111111111</str_PeopleUnit>
</PeopleInfoList>
</PeopleInfo>
一:创建主架构的 XML 文件
XmlWriterSettings settring = new XmlWriterSettings ();
settring .Indent = true ;
settring .NewLineOnAttributes = true ;
XmlWriter writer = XmlWriter .Create (CommonUseXMLPrevFilePath , settring );
writer .WriteStartDocument ();
writer .WriteStartElement ("PeopleInfo" );
writer .WriteEndElement ();
writer .WriteEndDocument ();
writer .Flush ();
writer .Close ();
二:添加数据
if (!File .Exists (CommonUseXMLPrevFilePath ))
{
CreateXMLFile ();
}
this .document .Load (CommonUseXMLPrevFilePath );
XmlElement vehNode = this .document .CreateElement ("PeopleInfoList" );
vehNode .SetAttribute ("str_Epc" , epc );
vehNode .SetAttribute ("str_name" , "chp" );
XmlElement prev_livestock_ID = this .document .CreateElement ("str_PeopleID" );
prev_livestock_ID .InnerText = id ;
XmlElement prev_time = this .document .CreateElement ("str_PeopleName" );
prev_time .InnerText = name ;
XmlElement prev_address = this .document .CreateElement ("str_PeopleUnit" );
prev_address .InnerText = unit ;
vehNode .AppendChild (prev_livestock_ID );
vehNode .AppendChild (prev_time );
vehNode .AppendChild (prev_address );
//this.document.GetElementsByTagName("PeopleInfo")[0].AppendChild(vehNode);
this .document .SelectSingleNode ("PeopleInfo" ).AppendChild (vehNode );
this .document .Save (CommonUseXMLPrevFilePath );
三:修改数据
if (!File .Exists (CommonUseXMLPrevFilePath ))
{
CreateXMLFile ();
}
this .document .Load (CommonUseXMLPrevFilePath );
XmlNodeList nodeList = this .document .SelectSingleNode ("PeopleInfo" ).ChildNodes ;
foreach (XmlNode xn in nodeList )
{
XmlElement xe = (XmlElement )xn ;
XmlNodeList nls = xe .ChildNodes ;
foreach (XmlNode xn1 in nls )
{
XmlElement xe2 = (XmlElement )xn1 ;
if (xe2 .Name == "str_PeopleUnit" )
{
xe2 .InnerText = "11111111111" ;
}
}
//if (xe.GetAttribute("str_name") == "chp")
//{
//xe.SetAttribute("str_name", "chpdirect");
//XmlNodeList nls = xe.ChildNodes;
//foreach (XmlNode xn1 in nls)
//{
// XmlElement xe2 = (XmlElement)xn1;
// if (xe2.Name == "str_PeopleUnit")
// {
// xe2.InnerText = "00000000";
// }
//}
//}
}
this .document .Save (CommonUseXMLPrevFilePath );// 保存。
四:删除
if (!File .Exists (CommonUseXMLPrevFilePath ))
{
CreateXMLFile ();
}
this .document .Load (CommonUseXMLPrevFilePath );
XmlNodeList nodeList = this .document .SelectSingleNode ("PeopleInfo" ).ChildNodes ;
foreach (XmlNode xn in nodeList )
{
XmlElement xe = (XmlElement )xn ;
if (xe .GetAttribute ("str_Epc" ) == "350000000000000000000100" )
{
xe .RemoveAttribute ("str_Epc" );
}
else if (xe .GetAttribute ("str_Epc" ) == "350000000000000000000101" )
{
//xe.RemoveAllAttributes();
xe .RemoveAll ();
}
}
this .document .Save (CommonUseXMLPrevFilePath );// 保存。
- XML基本读,写,删除操作
- c#操作XML(读XML,写XML,更新,删除节...
- c#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)
- c#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)
- c#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)
- c#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)
- C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)
- C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)
- c#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)
- C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)
- XML创建、读、写操作
- XML创建、读、写操作
- XML创建、读、写操作
- C#操作XML(读,写,更新,删除节点,与dataset结合等) 转
- 在php中XML 的读,写,删除,修改的操作
- C#操作Xml全集示例篇,基本实现添加,删除,更新等操作
- C#操作Xml全集示例篇,基本实现添加,删除,更新等操作
- XML基本操作
- 一个Java程序员应该掌握的10项技能
- CMarkup 11 STL 版本使用
- 关于保护源代码安全建议
- Selenium学习笔记5
- PMP考试总结
- XML基本读,写,删除操作
- Mac OS X 10.5快速系统部署实践-制作核心系统镜像(3) v1
- Mental fitness
- fread和fwrite
- 遨游 添加各种搜索引擎
- 程序设计2
- WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
- 第二章 变量与基本类型
- PC端远程调用WINCE中DLL失败的解决办法!!!